Skip to content

Commit 0d49b6a

Browse files
authored
Merge pull request #495 from nicpillinger/language-server-should-support-ignore-files
pass ignore files cli options to language server
2 parents 4aad240 + 3844084 commit 0d49b6a

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

lib/syntax_tree/cli.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,10 @@ def run(argv)
593593
when "j", "json"
594594
Json.new(options)
595595
when "lsp"
596-
LanguageServer.new(print_width: options.print_width).run
596+
LanguageServer.new(
597+
print_width: options.print_width,
598+
ignore_files: options.ignore_files
599+
).run
597600
return 0
598601
when "m", "match"
599602
Match.new(options)

lib/syntax_tree/language_server.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,13 @@ def self.[](value)
217217
def initialize(
218218
input: $stdin,
219219
output: $stdout,
220-
print_width: DEFAULT_PRINT_WIDTH
220+
print_width: DEFAULT_PRINT_WIDTH,
221+
ignore_files: []
221222
)
222223
@input = input.binmode
223224
@output = output.binmode
224225
@print_width = print_width
226+
@ignore_files = ignore_files
225227
end
226228

227229
# rubocop:disable Layout/LineLength
@@ -255,8 +257,12 @@ def run
255257
store.delete(request.dig(:params, :textDocument, :uri))
256258
when Request[method: "textDocument/formatting", id: :any, params: { textDocument: { uri: :any } }]
257259
uri = request.dig(:params, :textDocument, :uri)
260+
filepath = uri.split("///").last
261+
ignore = @ignore_files.any? do |glob|
262+
File.fnmatch(glob, filepath)
263+
end
258264
contents = store[uri]
259-
write(id: request[:id], result: contents ? format(contents, uri.split(".").last) : nil)
265+
write(id: request[:id], result: contents && !ignore ? format(contents, uri.split(".").last) : nil)
260266
when Request[method: "textDocument/inlayHint", id: :any, params: { textDocument: { uri: :any } }]
261267
uri = request.dig(:params, :textDocument, :uri)
262268
contents = store[uri]

test/language_server_test.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,24 @@ def test_formatting
151151
assert_equal("class Bar\nend\n", responses.dig(1, :result, 0, :newText))
152152
end
153153

154+
def test_formatting_ignore
155+
responses = run_server([
156+
Initialize.new(1),
157+
TextDocumentDidOpen.new("file:///path/to/file.rb", "class Foo; end"),
158+
TextDocumentFormatting.new(2, "file:///path/to/file.rb"),
159+
Shutdown.new(3)
160+
], ignore_files: ["path/**/*.rb"])
161+
162+
shape = LanguageServer::Request[[
163+
{ id: 1, result: { capabilities: Hash } },
164+
{ id: 2, result: :any },
165+
{ id: 3, result: {} }
166+
]]
167+
168+
assert_operator(shape, :===, responses)
169+
assert_nil(responses.dig(1, :result))
170+
end
171+
154172
def test_formatting_failure
155173
responses = run_server([
156174
Initialize.new(1),
@@ -322,14 +340,15 @@ def read(content)
322340
end
323341
end
324342

325-
def run_server(messages, print_width: DEFAULT_PRINT_WIDTH)
343+
def run_server(messages, print_width: DEFAULT_PRINT_WIDTH, ignore_files: [])
326344
input = StringIO.new(messages.map { |message| write(message) }.join)
327345
output = StringIO.new
328346

329347
LanguageServer.new(
330348
input: input,
331349
output: output,
332-
print_width: print_width
350+
print_width: print_width,
351+
ignore_files: ignore_files
333352
).run
334353

335354
read(output.tap(&:rewind))

0 commit comments

Comments
 (0)