Skip to content

Commit

Permalink
Merge pull request #109 from ruby-syntax-tree/fix-102
Browse files Browse the repository at this point in the history
Fix for #102, handle files not existing
  • Loading branch information
kddnewton authored Jul 5, 2022
2 parents c9ce212 + f8839b4 commit fbccb76
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 34 deletions.
4 changes: 3 additions & 1 deletion lib/syntax_tree/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ def format(node, stackable: true)
# going to just print out the node as it was seen in the source.
doc =
if leading.last&.ignore?
text(source[node.location.start_char...node.location.end_char])
range = source[node.location.start_char...node.location.end_char]
separator = -> { breakable(indent: false, force: true) }
seplist(range.split(/\r?\n/, -1), separator) { |line| text(line) }
else
node.format(self)
end
Expand Down
51 changes: 18 additions & 33 deletions lib/syntax_tree/language_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,66 +20,51 @@ def initialize(input: $stdin, output: $stdout)
@output = output.binmode
end

# rubocop:disable Layout/LineLength
def run
store =
Hash.new do |hash, uri|
hash[uri] = File.binread(CGI.unescape(URI.parse(uri).path))
filepath = CGI.unescape(URI.parse(uri).path)
File.exist?(filepath) ? (hash[uri] = File.read(filepath)) : nil
end

while (headers = input.gets("\r\n\r\n"))
source = input.read(headers[/Content-Length: (\d+)/i, 1].to_i)
request = JSON.parse(source, symbolize_names: true)

# stree-ignore
case request
in { method: "initialize", id: }
store.clear
write(id: id, result: { capabilities: capabilities })
in method: "initialized"
in { method: "initialized" }
# ignored
in method: "shutdown" # tolerate missing ID to be a good citizen
in { method: "shutdown" } # tolerate missing ID to be a good citizen
store.clear
write(id: request[:id], result: {})
return
in {
method: "textDocument/didChange",
params: { textDocument: { uri: }, contentChanges: [{ text: }, *] }
}
in { method: "textDocument/didChange", params: { textDocument: { uri: }, contentChanges: [{ text: }, *] } }
store[uri] = text
in {
method: "textDocument/didOpen",
params: { textDocument: { uri:, text: } }
}
in { method: "textDocument/didOpen", params: { textDocument: { uri:, text: } } }
store[uri] = text
in {
method: "textDocument/didClose", params: { textDocument: { uri: } }
}
in { method: "textDocument/didClose", params: { textDocument: { uri: } } }
store.delete(uri)
in {
method: "textDocument/formatting",
id:,
params: { textDocument: { uri: } }
}
write(id: id, result: [format(store[uri])])
in {
# official RPC in LSP spec 3.17
method: "textDocument/inlayHint",
id:,
params: { textDocument: { uri: } }
}
write(id: id, result: inlay_hints(store[uri]))
in {
method: "syntaxTree/visualizing",
id:,
params: { textDocument: { uri: } }
}
in { method: "textDocument/formatting", id:, params: { textDocument: { uri: } } }
contents = store[uri]
write(id: id, result: contents ? [format(store[uri])] : nil)
in { method: "textDocument/inlayHint", id:, params: { textDocument: { uri: } } }
contents = store[uri]
write(id: id, result: contents ? inlay_hints(store[uri]) : nil)
in { method: "syntaxTree/visualizing", id:, params: { textDocument: { uri: } } }
write(id: id, result: PP.pp(SyntaxTree.parse(store[uri]), +""))
in method: %r{\$/.+}
in { method: %r{\$/.+} }
# ignored
else
raise ArgumentError, "Unhandled: #{request}"
end
end
end
# rubocop:enable Layout/LineLength

private

Expand Down
17 changes: 17 additions & 0 deletions test/language_server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,23 @@ def test_clean_shutdown
end
end

def test_file_that_does_not_exist
messages = [
Initialize.new(1),
TextDocumentFormatting.new(2, "file:///path/to/file.rb"),
Shutdown.new(3)
]

case run_server(messages)
in [
{ id: 1, result: { capabilities: Hash } },
{ id: 2, result: nil },
{ id: 3, result: {} }
]
assert_equal(true, true)
end
end

private

def write(content)
Expand Down

0 comments on commit fbccb76

Please sign in to comment.