feat: add asynchronous LSP plugin diagnostics and definitions
This commit is contained in:
+138
-18
@@ -8,6 +8,7 @@ protocol_in = STDIN.dup
|
||||
protocol_in.binmode
|
||||
STDOUT.reopen(STDERR)
|
||||
$stdout = STDERR
|
||||
Thread.abort_on_exception = true
|
||||
|
||||
module Vamp
|
||||
MAX_FRAME = 4 * 1024 * 1024
|
||||
@@ -18,6 +19,37 @@ module Vamp
|
||||
value.dup.force_encoding(Encoding::UTF_8)
|
||||
end
|
||||
|
||||
def self.json_value!(value)
|
||||
case value
|
||||
when Hash
|
||||
value.to_h do |key, item|
|
||||
raise TypeError, "option keys must be Strings or Symbols" unless key.is_a?(String) || key.is_a?(Symbol)
|
||||
[string!(key.to_s, "option key"), json_value!(item)]
|
||||
end
|
||||
when Array
|
||||
value.map { |item| json_value!(item) }
|
||||
when String
|
||||
string!(value, "option value")
|
||||
when Integer, TrueClass, FalseClass, NilClass
|
||||
value
|
||||
when Float
|
||||
raise ArgumentError, "option numbers must be finite" unless value.finite?
|
||||
value
|
||||
else
|
||||
raise TypeError, "options must contain only JSON values"
|
||||
end
|
||||
end
|
||||
|
||||
def self.deep_freeze(value)
|
||||
case value
|
||||
when Hash
|
||||
value.each { |key, item| deep_freeze(key); deep_freeze(item) }
|
||||
when Array
|
||||
value.each { |item| deep_freeze(item) }
|
||||
end
|
||||
value.freeze
|
||||
end
|
||||
|
||||
Action = Struct.new(:name)
|
||||
Mode = Struct.new(:name)
|
||||
|
||||
@@ -59,10 +91,11 @@ module Vamp
|
||||
@clipboard_mode = "system"
|
||||
end
|
||||
|
||||
def plugin(path)
|
||||
def plugin(path, options: {})
|
||||
path = Vamp.string!(path, "plugin path")
|
||||
raise ArgumentError, "plugin path must not be empty" if path.empty?
|
||||
@plugins << path
|
||||
raise ArgumentError, "plugin path must not be empty" if path.empty? || path.include?("\0")
|
||||
raise TypeError, "plugin options must be a Hash" unless options.is_a?(Hash)
|
||||
@plugins << { "path" => path, "options" => Vamp.json_value!(options) }
|
||||
nil
|
||||
end
|
||||
|
||||
@@ -136,20 +169,27 @@ module Vamp
|
||||
end
|
||||
|
||||
class Editor
|
||||
attr_reader :text, :row, :column, :mode, :path, :effects
|
||||
attr_reader :text, :row, :column, :mode, :path, :effects, :buffer_id, :revision, :root, :request_id
|
||||
|
||||
def initialize(context)
|
||||
unless context.is_a?(Hash) && context.keys.sort == %w[column mode path row text]
|
||||
def initialize(context, request_id)
|
||||
unless context.is_a?(Hash) && context.keys.sort == %w[buffer_id column mode path revision root row text]
|
||||
raise ArgumentError, "invalid editor context"
|
||||
end
|
||||
@text = Vamp.string!(context["text"], "context text").freeze
|
||||
@mode = Vamp.string!(context["mode"], "context mode").freeze
|
||||
@path = context["path"].nil? ? nil : Vamp.string!(context["path"], "context path").freeze
|
||||
@root = Vamp.string!(context["root"], "context root").freeze
|
||||
@buffer_id = context["buffer_id"]
|
||||
@revision = context["revision"]
|
||||
@request_id = request_id
|
||||
@row = context["row"]
|
||||
@column = context["column"]
|
||||
unless @row.is_a?(Integer) && @row > 0 && @column.is_a?(Integer) && @column > 0
|
||||
raise ArgumentError, "context row and column must be positive integers"
|
||||
end
|
||||
unless @buffer_id.is_a?(Integer) && @buffer_id >= 0 && @revision.is_a?(Integer) && @revision >= 0
|
||||
raise ArgumentError, "context buffer_id and revision must be nonnegative integers"
|
||||
end
|
||||
@effects = []
|
||||
@effect_bytes = 0
|
||||
end
|
||||
@@ -177,12 +217,22 @@ module Vamp
|
||||
end
|
||||
end
|
||||
|
||||
def self.setup(mode)
|
||||
def self.setup(mode, options, sender)
|
||||
raise TypeError, "plugin options must be a Hash" unless options.is_a?(Hash)
|
||||
@mode = mode
|
||||
@config = Config.new
|
||||
@name = nil
|
||||
@requires = []
|
||||
@commands = {}
|
||||
@async = false
|
||||
@document_callback = nil
|
||||
@shutdown_callback = nil
|
||||
@options = deep_freeze(json_value!(options))
|
||||
@sender = sender
|
||||
end
|
||||
|
||||
def self.options
|
||||
@options
|
||||
end
|
||||
|
||||
def self.configure
|
||||
@@ -191,7 +241,7 @@ module Vamp
|
||||
yield @config
|
||||
end
|
||||
|
||||
def self.plugin(name, requires: [])
|
||||
def self.plugin(name, requires: [], async: false)
|
||||
raise ArgumentError, "plugin is only available in plugin files" unless @mode == "plugin"
|
||||
raise ArgumentError, "plugin already declared" unless @name.nil?
|
||||
name = string!(name, "plugin name")
|
||||
@@ -199,6 +249,8 @@ module Vamp
|
||||
raise TypeError, "requires must be an Array of Strings" unless requires.is_a?(Array)
|
||||
@requires = requires.map { |value| string!(value, "capability") }
|
||||
raise ArgumentError, "capability must not be empty" if @requires.any?(&:empty?)
|
||||
raise TypeError, "async must be true or false" unless async == true || async == false
|
||||
@async = async
|
||||
@name = name
|
||||
nil
|
||||
end
|
||||
@@ -213,11 +265,62 @@ module Vamp
|
||||
nil
|
||||
end
|
||||
|
||||
def self.on_document(&callback)
|
||||
raise ArgumentError, "document callbacks require an asynchronous plugin" unless @mode == "plugin" && @name && @async
|
||||
raise ArgumentError, "on_document requires a block" unless callback
|
||||
raise ArgumentError, "document callback already registered" if @document_callback
|
||||
@document_callback = callback
|
||||
nil
|
||||
end
|
||||
|
||||
def self.on_shutdown(&callback)
|
||||
raise ArgumentError, "shutdown callbacks require an asynchronous plugin" unless @mode == "plugin" && @name && @async
|
||||
raise ArgumentError, "on_shutdown requires a block" unless callback
|
||||
raise ArgumentError, "shutdown callback already registered" if @shutdown_callback
|
||||
@shutdown_callback = callback
|
||||
nil
|
||||
end
|
||||
|
||||
def self.shutdown
|
||||
callback, @shutdown_callback = @shutdown_callback, nil
|
||||
callback&.call
|
||||
end
|
||||
|
||||
def self.publish(effects, request_id: nil)
|
||||
raise ArgumentError, "publish requires an asynchronous plugin" unless @mode == "plugin" && @name && @async
|
||||
raise TypeError, "published effects must be an Array of Hashes" unless effects.is_a?(Array) && effects.all? { |effect| effect.is_a?(Hash) }
|
||||
unless request_id.nil? || (request_id.is_a?(Integer) && request_id > 0 && request_id <= 18_446_744_073_709_551_615)
|
||||
raise ArgumentError, "request_id must be a positive unsigned 64-bit integer or nil"
|
||||
end
|
||||
@sender.call({ "version" => 1, "type" => "update", "request_id" => request_id,
|
||||
"effects" => json_value!(effects) })
|
||||
nil
|
||||
end
|
||||
|
||||
def self.document(request)
|
||||
unless @async && request.is_a?(Hash) && request.keys.sort == %w[document event type version] &&
|
||||
request["version"].is_a?(Integer) && request["version"] == 1 && request["type"] == "document" &&
|
||||
%w[open change save close].include?(request["event"])
|
||||
raise ArgumentError, "invalid document event frame"
|
||||
end
|
||||
document = request["document"]
|
||||
unless document.is_a?(Hash) && document.keys.sort == %w[id path revision root text] &&
|
||||
document["id"].is_a?(Integer) && document["id"] >= 0 &&
|
||||
document["revision"].is_a?(Integer) && document["revision"] >= 0
|
||||
raise ArgumentError, "invalid document snapshot"
|
||||
end
|
||||
document["text"] = string!(document["text"], "document text")
|
||||
document["root"] = string!(document["root"], "document root")
|
||||
document["path"] = string!(document["path"], "document path") unless document["path"].nil?
|
||||
@document_callback&.call(deep_freeze({ "event" => request["event"], "document" => document }))
|
||||
nil
|
||||
end
|
||||
|
||||
def self.startup
|
||||
return @config.response if @mode == "config"
|
||||
raise ArgumentError, "plugin file must declare Vamp.plugin" unless @name
|
||||
{ "version" => 1, "type" => "manifest", "name" => @name,
|
||||
"requires" => @requires, "commands" => @commands.keys }
|
||||
"requires" => @requires, "commands" => @commands.keys, "async" => @async }
|
||||
end
|
||||
|
||||
def self.invoke(request)
|
||||
@@ -230,24 +333,28 @@ module Vamp
|
||||
args = string!(request["args"], "args")
|
||||
callback = @commands[command]
|
||||
raise ArgumentError, "unknown command #{command}" unless callback
|
||||
editor = Editor.new(request["context"])
|
||||
editor = Editor.new(request["context"], request["id"])
|
||||
callback.call(editor, args)
|
||||
{ "version" => 1, "type" => "result", "id" => request["id"], "effects" => editor.effects }
|
||||
end
|
||||
end
|
||||
|
||||
protocol_mutex = Mutex.new
|
||||
send_frame = lambda do |frame|
|
||||
payload = JSON.generate(frame)
|
||||
raise ArgumentError, "protocol frame exceeds 4 MiB" if payload.bytesize > Vamp::MAX_FRAME
|
||||
protocol_out.write(payload)
|
||||
protocol_out.write("\n")
|
||||
protocol_mutex.synchronize do
|
||||
payload = JSON.generate(frame)
|
||||
raise ArgumentError, "protocol frame exceeds 4 MiB" if payload.bytesize > Vamp::MAX_FRAME
|
||||
protocol_out.write(payload)
|
||||
protocol_out.write("\n")
|
||||
end
|
||||
end
|
||||
|
||||
request_id = nil
|
||||
begin
|
||||
mode, path = ARGV
|
||||
raise ArgumentError, "expected config or plugin mode and a path" unless %w[config plugin].include?(mode) && path
|
||||
Vamp.setup(mode)
|
||||
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)
|
||||
load path
|
||||
send_frame.call(Vamp.startup)
|
||||
if mode == "plugin"
|
||||
@@ -260,9 +367,22 @@ begin
|
||||
end
|
||||
request = JSON.parse(line)
|
||||
request_id = request["id"] if request.is_a?(Hash) && request["id"].is_a?(Integer) && request["id"] > 0
|
||||
send_frame.call(Vamp.invoke(request))
|
||||
case request.is_a?(Hash) && request["type"]
|
||||
when "invoke"
|
||||
send_frame.call(Vamp.invoke(request))
|
||||
when "document"
|
||||
Vamp.document(request)
|
||||
when "shutdown"
|
||||
unless request.keys.sort == %w[type version] && request["version"].is_a?(Integer) && request["version"] == 1
|
||||
raise ArgumentError, "invalid shutdown frame"
|
||||
end
|
||||
break
|
||||
else
|
||||
raise ArgumentError, "unknown protocol frame"
|
||||
end
|
||||
end
|
||||
end
|
||||
Vamp.shutdown
|
||||
rescue Exception => error
|
||||
# No callback effects are emitted when loading or invoking raises.
|
||||
message = "#{error.class}: #{error.message}".encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
|
||||
|
||||
Reference in New Issue
Block a user