Skip to content

Commit

Permalink
Add support for folding defs
Browse files Browse the repository at this point in the history
Co-authored-by: Vinicius Stock <vinistock@users.noreply.github.com>
  • Loading branch information
Ryan Brushett and vinistock committed Mar 19, 2022
1 parent 1267876 commit 4c8a9ac
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/ruby/lsp.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true

require_relative "./lsp/cli"
require "syntax_tree"
38 changes: 36 additions & 2 deletions lib/ruby/lsp/cli.rb
Expand Up @@ -11,8 +11,42 @@ def self.start(_argv)
handler = Ruby::Lsp::Handler.new

handler.config do
on("initialize") { respond_with_capabilities }
on("shutdown") { shutdown }
on("initialize") do
store.clear
respond_with_capabilities
end

on("textDocument/didChange") do |request|
uri = request.dig(:params, :textDocument, :uri)
text = request.dig(:params, :contentChanges, 0, :text)
store[uri] = text

nil
end

on("textDocument/didOpen") do |request|
uri = request.dig(:params, :textDocument, :uri)
text = request.dig(:params, :textDocument, :text)
store[uri] = text

nil
end

on("textDocument/didClose") do |request|
uri = request.dig(:params, :textDocument, :uri)
store.delete(uri)

nil
end

on("textDocument/foldingRange") do |request|
respond_with_folding_ranges(request.dig(:params, :textDocument, :uri))
end

on("shutdown") do
store.clear
shutdown
end
end

handler.start
Expand Down
17 changes: 17 additions & 0 deletions lib/ruby/lsp/handler.rb
@@ -1,8 +1,14 @@
# frozen_string_literal: true

require "cgi"
require "uri"
require_relative "./requests/folding/ranges"

module Ruby
module Lsp
class Handler
attr_reader :store

Interface = LanguageServer::Protocol::Interface
Constant = LanguageServer::Protocol::Constant
Transport = LanguageServer::Protocol::Transport
Expand All @@ -12,12 +18,16 @@ def initialize
@reader = Transport::Stdio::Reader.new
@handlers = {}
@running = true
@store = Hash.new do |hash, uri|
hash[uri] = File.binread(CGI.unescape(URI.parse(uri).path))
end
end

def start
$stderr.puts "Starting Ruby LSP..."
@reader.read do |request|
break unless @running

handle(request)
end
end
Expand Down Expand Up @@ -52,10 +62,17 @@ def respond_with_capabilities
resolve_provider: true,
trigger_characters: ["."]
),
folding_range_provider: Interface::FoldingRangeClientCapabilities.new(
line_folding_only: true
),
definition_provider: true
)
)
end

def respond_with_folding_ranges(uri)
Ruby::Lsp::Requests::Folding::Ranges.run(store[uri])
end
end
end
end
22 changes: 22 additions & 0 deletions test/requests/folding_ranges_test.rb
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require "test_helper"

class FoldingRangesTest < Minitest::Test
def test_folding_ranges
ranges = [{ startLine: 0, endLine: 3, kind: "region" }]
assert_ranges(<<~RUBY, ranges)
def foo
a = 2
puts "a"
end
RUBY
end

private

def assert_ranges(source, expected_ranges)
actual = Ruby::Lsp::Requests::Folding::Ranges.run(source)
assert_equal(expected_ranges, JSON.parse(actual.to_json, symbolize_names: true))
end
end

0 comments on commit 4c8a9ac

Please sign in to comment.