refactor: derive Ruby action namespaces from Rust catalog
This commit is contained in:
@@ -476,6 +476,15 @@ const CORE_ACTIONS: &[(&str, Action, &[Mode])] = &[
|
||||
),
|
||||
];
|
||||
|
||||
/// Serializes the authoritative action names without copying the catalog.
|
||||
pub(crate) struct CoreActionCatalog;
|
||||
|
||||
impl Serialize for CoreActionCatalog {
|
||||
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
serializer.collect_seq(CORE_ACTIONS.iter().map(|(name, _, _)| *name))
|
||||
}
|
||||
}
|
||||
|
||||
impl Action {
|
||||
fn named(mode: Mode, name: &str) -> Option<Self> {
|
||||
CORE_ACTIONS
|
||||
|
||||
+17
-4
@@ -463,15 +463,26 @@ struct Host {
|
||||
|
||||
impl Host {
|
||||
fn spawn(mode: &str, path: &Path, options: &serde_json::Value) -> Result<Self, String> {
|
||||
let mut encoded_options = FrameWriter(Vec::new());
|
||||
serde_json::to_writer(&mut encoded_options, options)
|
||||
.map_err(|error| format!("Cannot encode bounded Ruby plugin options: {error}"))?;
|
||||
#[derive(Serialize)]
|
||||
struct Startup<'a> {
|
||||
core_actions: crate::editor::CoreActionCatalog,
|
||||
options: &'a serde_json::Value,
|
||||
}
|
||||
let mut encoded_startup = FrameWriter(Vec::new());
|
||||
serde_json::to_writer(
|
||||
&mut encoded_startup,
|
||||
&Startup {
|
||||
core_actions: crate::editor::CoreActionCatalog,
|
||||
options,
|
||||
},
|
||||
)
|
||||
.map_err(|error| format!("Cannot encode bounded Ruby plugin options: {error}"))?;
|
||||
let executable = env::var_os("VAMP_RUBY").unwrap_or_else(|| "ruby".into());
|
||||
let mut command = Command::new(&executable);
|
||||
command
|
||||
.args(["-e", RUNNER, "--", mode])
|
||||
.arg(path)
|
||||
.arg(String::from_utf8(encoded_options.0).expect("JSON is UTF-8"))
|
||||
.arg(String::from_utf8(encoded_startup.0).expect("JSON is UTF-8"))
|
||||
.current_dir(path.parent().expect("absolute file has a parent"))
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
@@ -879,6 +890,8 @@ end
|
||||
"config.core.lsp_diagnostics",
|
||||
"config.core.lsp.hvoer",
|
||||
"config.core.lsp",
|
||||
"config.core.lsp.hover.tap { |action| action.name = 'core.delete' }",
|
||||
"config.core.lsp.tap { |namespace| namespace.define_singleton_method(:bogus) { namespace.hover } }.bogus",
|
||||
] {
|
||||
let invalid = fixture.file(
|
||||
"invalid_action.rb",
|
||||
|
||||
+36
-30
@@ -53,33 +53,35 @@ module Vamp
|
||||
Action = Struct.new(:name)
|
||||
Mode = Struct.new(:name)
|
||||
|
||||
module Core
|
||||
%w[
|
||||
insert insert_first_nonblank append append_line_end open_below open_above
|
||||
left right down up join_lines line_start line_end delete undo redo command
|
||||
normal newline backspace submit palette repeat_last_edit
|
||||
search search_next search_previous
|
||||
word_next word_previous word_end big_word_next big_word_previous big_word_end
|
||||
select copy cut change paste_before paste_after
|
||||
copy_system paste_system_before paste_system_after
|
||||
select_lines clone_above clone_below select_next skip_occurrence select_all
|
||||
file_picker file_explorer
|
||||
].each do |name|
|
||||
action = Action.new("core.#{name}".freeze).freeze
|
||||
define_singleton_method(name) { action }
|
||||
end
|
||||
Core = Module.new
|
||||
|
||||
module Lsp
|
||||
%w[definition diagnostics hover].each do |name|
|
||||
action = Action.new("core.lsp.#{name}".freeze).freeze
|
||||
define_singleton_method(name) { action }
|
||||
def self.install_actions(names)
|
||||
raise TypeError, "core action catalog must be an Array" unless names.is_a?(Array)
|
||||
namespaces = [Core]
|
||||
names.each do |name|
|
||||
name = string!(name, "core action name")
|
||||
unless name.match?(/\Acore(?:\.[a-z_][a-z0-9_]*)+\z/)
|
||||
raise ArgumentError, "invalid core action name: #{name}"
|
||||
end
|
||||
parts = name.split(".").drop(1)
|
||||
namespace = Core
|
||||
parts[0...-1].each do |part|
|
||||
if namespace.singleton_methods(false).include?(part.to_sym)
|
||||
child = namespace.public_send(part)
|
||||
raise ArgumentError, "conflicting core action namespace: #{name}" unless child.is_a?(Module)
|
||||
else
|
||||
child = Module.new
|
||||
namespace.define_singleton_method(part) { child }
|
||||
namespaces << child
|
||||
end
|
||||
namespace = child
|
||||
end
|
||||
leaf = parts.last
|
||||
raise ArgumentError, "conflicting core action name: #{name}" if namespace.singleton_methods(false).include?(leaf.to_sym)
|
||||
action = Action.send(:new, name.freeze).freeze
|
||||
namespace.define_singleton_method(leaf) { action }
|
||||
end
|
||||
Lsp.freeze
|
||||
|
||||
def self.lsp
|
||||
Lsp
|
||||
end
|
||||
namespaces.each(&:freeze)
|
||||
end
|
||||
|
||||
module Modes
|
||||
@@ -247,8 +249,12 @@ module Vamp
|
||||
end
|
||||
end
|
||||
|
||||
def self.setup(mode, options, sender)
|
||||
raise TypeError, "plugin options must be a Hash" unless options.is_a?(Hash)
|
||||
def self.setup(mode, startup, sender)
|
||||
unless startup.is_a?(Hash) && startup.keys.sort == %w[core_actions options] && startup["options"].is_a?(Hash)
|
||||
raise TypeError, "invalid Ruby startup catalog/options"
|
||||
end
|
||||
install_actions(startup["core_actions"])
|
||||
options = startup["options"]
|
||||
@mode = mode
|
||||
@config = Config.new
|
||||
@name = nil
|
||||
@@ -381,10 +387,10 @@ end
|
||||
|
||||
request_id = nil
|
||||
begin
|
||||
mode, path, encoded_options = ARGV
|
||||
raise ArgumentError, "expected config or plugin mode, path, and options" unless %w[config plugin].include?(mode) && path && encoded_options
|
||||
raise ArgumentError, "plugin options exceed 4 MiB" if encoded_options.bytesize > Vamp::MAX_FRAME
|
||||
Vamp.setup(mode, JSON.parse(encoded_options), send_frame)
|
||||
mode, path, encoded_startup = ARGV
|
||||
raise ArgumentError, "expected config or plugin mode, path, and startup data" unless %w[config plugin].include?(mode) && path && encoded_startup
|
||||
raise ArgumentError, "Ruby startup data exceeds 4 MiB" if encoded_startup.bytesize > Vamp::MAX_FRAME
|
||||
Vamp.setup(mode, JSON.parse(encoded_startup), send_frame)
|
||||
load path
|
||||
send_frame.call(Vamp.startup)
|
||||
if mode == "plugin"
|
||||
|
||||
Reference in New Issue
Block a user