Skip to content

pass ignore files cli options to language server #495

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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: 2 additions & 1 deletion lib/syntax_tree/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,8 @@ def run(argv)
when "j", "json"
Json.new(options)
when "lsp"
LanguageServer.new(print_width: options.print_width).run
LanguageServer.new(ignore_files: options.ignore_files,
print_width: options.print_width).run
return 0
when "m", "match"
Match.new(options)
Expand Down
8 changes: 7 additions & 1 deletion lib/syntax_tree/language_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,12 @@ def self.[](value)
attr_reader :input, :output, :print_width

def initialize(
ignore_files: [],
input: $stdin,
output: $stdout,
print_width: DEFAULT_PRINT_WIDTH
)
@ignore_files = ignore_files
@input = input.binmode
@output = output.binmode
@print_width = print_width
Expand Down Expand Up @@ -255,8 +257,12 @@ def run
store.delete(request.dig(:params, :textDocument, :uri))
when Request[method: "textDocument/formatting", id: :any, params: { textDocument: { uri: :any } }]
uri = request.dig(:params, :textDocument, :uri)
filepath = uri.split("///").last
ignore = @ignore_files.any? do |glob|
File.fnmatch(glob, filepath)
end
contents = store[uri]
write(id: request[:id], result: contents ? format(contents, uri.split(".").last) : nil)
write(id: request[:id], result: contents && !ignore ? 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]
Expand Down
21 changes: 20 additions & 1 deletion test/language_server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,24 @@ def test_formatting
assert_equal("class Bar\nend\n", responses.dig(1, :result, 0, :newText))
end

def test_formatting_ignore
responses = run_server([
Initialize.new(1),
TextDocumentDidOpen.new("file:///path/to/file.rb", "class Foo; end"),
TextDocumentFormatting.new(2, "file:///path/to/file.rb"),
Shutdown.new(3)
], ignore_files: ["path/**/*.rb"])

shape = LanguageServer::Request[[
{ id: 1, result: { capabilities: Hash } },
{ id: 2, result: :any },
{ id: 3, result: {} }
]]

assert_operator(shape, :===, responses)
assert_nil(responses.dig(1, :result))
end

def test_formatting_failure
responses = run_server([
Initialize.new(1),
Expand Down Expand Up @@ -322,11 +340,12 @@ def read(content)
end
end

def run_server(messages, print_width: DEFAULT_PRINT_WIDTH)
def run_server(messages, ignore_files: [], print_width: DEFAULT_PRINT_WIDTH)
input = StringIO.new(messages.map { |message| write(message) }.join)
output = StringIO.new

LanguageServer.new(
ignore_files: ignore_files,
input: input,
output: output,
print_width: print_width
Expand Down