Skip to content

Style, pattern matching, rake exit status #184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ Naming/MethodParameterName:
Naming/RescuedExceptionsVariableName:
PreferredName: error

Style/CaseEquality:
Enabled: false

Style/ExplicitBlockArgument:
Enabled: false

Expand Down
8 changes: 1 addition & 7 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ require "syntax_tree/rake_tasks"
Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
test_files = FileList["test/**/*_test.rb"]
if RUBY_ENGINE == "truffleruby"
# language_server.rb uses pattern matching
test_files -= FileList["test/language_server/*_test.rb"]
test_files -= FileList["test/language_server_test.rb"]
end
t.test_files = test_files
t.test_files = FileList["test/**/*_test.rb"]
end

task default: :test
Expand Down
4 changes: 2 additions & 2 deletions lib/syntax_tree/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ def run(item)
class Expr < Action
def run(item)
program = item.handler.parse(item.source)
if Program === program and expressions = program.statements.body and
expressions.size == 1

if (expressions = program.statements.body) && expressions.size == 1
puts expressions.first.construct_keys
else
warn("The input to `stree expr` must be a single expression.")
Expand Down
81 changes: 64 additions & 17 deletions lib/syntax_tree/language_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,50 @@ module SyntaxTree
# stree lsp
#
class LanguageServer
# This is a small module that effectively mirrors pattern matching. We're
# using it so that we can support truffleruby without having to ignore the
# language server.
module Request
# Represents a hash pattern.
class Shape
attr_reader :values

def initialize(values)
@values = values
end

def ===(other)
values.all? do |key, value|
value == :any ? other.key?(key) : value === other[key]
end
end
end

# Represents an array pattern.
class Tuple
attr_reader :values

def initialize(values)
@values = values
end

def ===(other)
values.each_with_index.all? { |value, index| value === other[index] }
end
end

def self.[](value)
case value
when Array
Tuple.new(value.map { |child| self[child] })
when Hash
Shape.new(value.transform_values { |child| self[child] })
else
value
end
end
end

attr_reader :input, :output, :print_width

def initialize(
Expand All @@ -39,30 +83,33 @@ def run

# stree-ignore
case request
in { method: "initialize", id: }
when Request[method: "initialize", id: :any]
store.clear
write(id: id, result: { capabilities: capabilities })
in { method: "initialized" }
write(id: request[:id], result: { capabilities: capabilities })
when Request[method: "initialized"]
# ignored
in { method: "shutdown" } # tolerate missing ID to be a good citizen
when Request[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: }, *] } }
store[uri] = text
in { method: "textDocument/didOpen", params: { textDocument: { uri:, text: } } }
store[uri] = text
in { method: "textDocument/didClose", params: { textDocument: { uri: } } }
store.delete(uri)
in { method: "textDocument/formatting", id:, params: { textDocument: { uri: } } }
when Request[method: "textDocument/didChange", params: { textDocument: { uri: :any }, contentChanges: [{ text: :any }] }]
store[request.dig(:params, :textDocument, :uri)] = request.dig(:params, :contentChanges, 0, :text)
when Request[method: "textDocument/didOpen", params: { textDocument: { uri: :any, text: :any } }]
store[request.dig(:params, :textDocument, :uri)] = request.dig(:params, :textDocument, :text)
when Request[method: "textDocument/didClose", params: { textDocument: { uri: :any } }]
store.delete(request.dig(:params, :textDocument, :uri))
when Request[method: "textDocument/formatting", id: :any, params: { textDocument: { uri: :any } }]
uri = request.dig(:params, :textDocument, :uri)
contents = store[uri]
write(id: id, result: contents ? format(contents, uri.split(".").last) : nil)
in { method: "textDocument/inlayHint", id:, params: { textDocument: { uri: } } }
write(id: request[:id], result: contents ? format(contents, uri.split(".").last) : nil)
when Request[method: "textDocument/inlayHint", id: :any, params: { textDocument: { uri: :any } }]
uri = request.dig(:params, :textDocument, :uri)
contents = store[uri]
write(id: id, result: contents ? inlay_hints(contents) : nil)
in { method: "syntaxTree/visualizing", id:, params: { textDocument: { uri: } } }
write(id: id, result: PP.pp(SyntaxTree.parse(store[uri]), +""))
in { method: %r{\$/.+} }
write(id: request[:id], result: contents ? inlay_hints(contents) : nil)
when Request[method: "syntaxTree/visualizing", id: :any, params: { textDocument: { uri: :any } }]
uri = request.dig(:params, :textDocument, :uri)
write(id: request[:id], result: PP.pp(SyntaxTree.parse(store[uri]), +""))
when Request[method: %r{\$/.+}]
# ignored
else
raise ArgumentError, "Unhandled: #{request}"
Expand Down
10 changes: 4 additions & 6 deletions lib/syntax_tree/language_server/inlay_hints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,10 @@ def visit_assign(node)
#
def visit_binary(node)
case stack[-2]
in Assign | OpAssign
when Assign, OpAssign
parentheses(node.location)
in Binary[operator: operator] if operator != node.operator
parentheses(node.location)
else
when Binary
parentheses(node.location) if stack[-2].operator != node.operator
end

super
Expand All @@ -91,9 +90,8 @@ def visit_binary(node)
#
def visit_if_op(node)
case stack[-2]
in Assign | Binary | IfOp | OpAssign
when Assign, Binary, IfOp, OpAssign
parentheses(node.location)
else
end

super
Expand Down
Loading