Class: RubySyntaxLsp
- Inherits:
-
Object
- Object
- RubySyntaxLsp
- Defined in:
- lib/ruby_syntax_lsp/cli.rb,
lib/ruby_syntax_lsp/core.rb,
lib/ruby_syntax_lsp/version.rb
Constant Summary collapse
- VERSION =
"0.0.3"
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(writer, reader, level) ⇒ RubySyntaxLsp
constructor
A new instance of RubySyntaxLsp.
- #request_initialize(id, params) ⇒ Object
- #request_initialized(id, params) ⇒ Object
- #request_text_document_did_change(id, params) ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(writer, reader, level) ⇒ RubySyntaxLsp
Returns a new instance of RubySyntaxLsp.
9 10 11 12 13 |
# File 'lib/ruby_syntax_lsp/core.rb', line 9 def initialize(writer, reader, level) @writer = writer @reader = reader @logger = Logger.new(STDERR, level: level) end |
Class Method Details
.cli ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ruby_syntax_lsp/cli.rb', line 8 def self.cli = { log_level: :info, } opts = OptionParser.new("Usage: ruby_syntax_lsp [options]") opts.on("--log LEVEL", "Set log level (debug, info, warn, error, fatal)") do |v| [:log_level] = level.to_sym end opts.on("--verbose", "Set log level to debug") do [:log_level] = :debug end opts.version = RubySyntaxLsp::VERSION opts.parse! reader = LSP::Transport::Stdio::Reader.new writer = LSP::Transport::Stdio::Writer.new RubySyntaxLsp.new(writer, reader, [:log_level]).start end |
Instance Method Details
#request_initialize(id, params) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/ruby_syntax_lsp/core.rb', line 42 def request_initialize(id, params) @writer.write( id: id, result: LSP::Interface::InitializeResult.new( capabilities: LSP::Interface::ServerCapabilities.new( text_document_sync: LSP::Interface::TextDocumentSyncOptions.new( change: LSP::Constant::TextDocumentSyncKind::FULL, ), ), ), ) end |
#request_initialized(id, params) ⇒ Object
55 56 57 |
# File 'lib/ruby_syntax_lsp/core.rb', line 55 def request_initialized(id, params) # none end |
#request_text_document_did_change(id, params) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ruby_syntax_lsp/core.rb', line 59 def request_text_document_did_change(id, params) @writer.write( method: "textDocument/publishDiagnostics", params: { uri: params[:textDocument][:uri], diagnostics: params[:contentChanges].filter_map do |change| text = change[:text] begin RubyVM::InstructionSequence.compile(text) rescue SyntaxError => e @logger.info("SyntaxError detected:\n#{e.}") lines = text.lines e..split(/^<compiled>:(?=\d+: )/m)[1..].map do |error| @logger.info("Parsing this error message:\n#{error}") lineno_s, = error.split(": ", 2) lineno = lineno_s.strip.to_i @logger.info("Line number: #{lineno}") range = if .lines.length > 1 # Message has `^` pos_start = .lines[2].delete_prefix("...").index("^") if .lines[1].start_with?("...") pos_start += lines[lineno - 1].index(.lines[1].delete_prefix("...").delete_suffix("...")) end pos_end = pos_start + .lines[2].count("~") + 1 @logger.info("line: #{lineno}, position: #{pos_start}-#{pos_end}") LSP::Interface::Range.new( start: LSP::Interface::Position.new(line: lineno - 1, character: pos_start), end: LSP::Interface::Position.new(line: lineno - 1, character: pos_end), ) else @logger.info("line: #{lineno}, no position") LSP::Interface::Range.new( start: LSP::Interface::Position.new(line: lineno - 1, character: 0), end: LSP::Interface::Position.new(line: lineno - 1, character: lines[lineno - 1].length - 1), ) end LSP::Interface::Diagnostic.new( range: range, severity: LSP::Constant::DiagnosticSeverity::ERROR, message: .lines[0].strip.sub(/^syntax error, /, ""), source: "Ruby Syntax LSP", ) end else @logger.info("No SyntaxError") nil end end.flatten, }, ) end |
#start ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/ruby_syntax_lsp/core.rb', line 15 def start @logger.info "Started Ruby Syntax LSP server..." @reader.read do |request| break if request[:method] == "shutdown" method_name = ("request_" + request[:method].gsub(/[A-Z]/) { |m| "_#{m.downcase}" }.gsub("/", "_")).to_sym if respond_to?(method_name) @logger.debug "Received supported method: #{method_name}" begin send(method_name, request[:id], request[:params]) rescue => e @logger.error "Error: #{e.}" @writer.write( code: -32603, message: e., ) end elsif request[:method].start_with?("$/") @logger.debug "Received unsupported method: #{request[:method]}" else @writer.write( code: -32601, message: "Method not implemented: #{request[:method]}", ) end end end |