Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Pass --print-width to LSP #116

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 1 commit into from
Jul 19, 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
4 changes: 2 additions & 2 deletions lib/syntax_tree/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def run(item)
#{Color.bold("stree help")}
Display this help message

#{Color.bold("stree lsp [--plugins=...]")}
#{Color.bold("stree lsp [--plugins=...] [--print-width=NUMBER]")}
Run syntax tree in language server mode

#{Color.bold("stree version")}
Expand Down Expand Up @@ -300,7 +300,7 @@ def run(argv)
return 0
when "lsp"
require "syntax_tree/language_server"
LanguageServer.new.run
LanguageServer.new(print_width: print_width).run
return 0
when "version"
puts SyntaxTree::VERSION
Expand Down
11 changes: 8 additions & 3 deletions lib/syntax_tree/language_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ module SyntaxTree
# stree lsp
#
class LanguageServer
attr_reader :input, :output
attr_reader :input, :output, :print_width

def initialize(input: $stdin, output: $stdout)
def initialize(
input: $stdin,
output: $stdout,
print_width: DEFAULT_PRINT_WIDTH
)
@input = input.binmode
@output = output.binmode
@print_width = print_width
end

# rubocop:disable Layout/LineLength
Expand Down Expand Up @@ -93,7 +98,7 @@ def format(source)
character: 0
}
},
newText: SyntaxTree.format(source)
newText: SyntaxTree.format(source, print_width)
}
end

Expand Down
28 changes: 26 additions & 2 deletions test/language_server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,26 @@ def test_formatting
end
end

def test_formatting_print_width
contents = "#{"a" * 40} + #{"b" * 40}\n"
messages = [
Initialize.new(1),
TextDocumentDidOpen.new("file:///path/to/file.rb", contents),
TextDocumentFormatting.new(2, "file:///path/to/file.rb"),
TextDocumentDidClose.new("file:///path/to/file.rb"),
Shutdown.new(3)
]

case run_server(messages, print_width: 100)
in [
{ id: 1, result: { capabilities: Hash } },
{ id: 2, result: [{ newText: new_text }] },
{ id: 3, result: {} }
]
assert_equal(contents, new_text)
end
end

def test_inlay_hint
messages = [
Initialize.new(1),
Expand Down Expand Up @@ -234,11 +254,15 @@ def read(content)
end
end

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

LanguageServer.new(input: input, output: output).run
LanguageServer.new(
input: input,
output: output,
print_width: print_width
).run
read(output.tap(&:rewind))
end
end
Expand Down