-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #195 from Shopify/support-document-link
Support DocumentLink request
- Loading branch information
Showing
9 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module RubyLsp | ||
module Requests | ||
# ![Document link demo](../../misc/document_link.gif) | ||
# | ||
# The [document link](https://microsoft.github.io/language-server-protocol/specification#textDocument_documentLink) | ||
# makes `# source://PATH_TO_FILE:line` comments in a Ruby/RBI file clickable if the file exists. | ||
# When the user clicks the link, it'll open that location. | ||
# | ||
# # Example | ||
# | ||
# ```ruby | ||
# # source://syntax_tree-3.2.1/lib/syntax_tree.rb:51 <- it will be clickable and will take the user to that location | ||
# def format(source, maxwidth = T.unsafe(nil)) | ||
# end | ||
# ``` | ||
class DocumentLink < BaseRequest | ||
extend T::Sig | ||
|
||
RUBY_ROOT = "RUBY_ROOT" | ||
|
||
sig { params(document: Document).void } | ||
def initialize(document) | ||
super | ||
|
||
@links = T.let([], T::Array[LanguageServer::Protocol::Interface::DocumentLink]) | ||
end | ||
|
||
sig { override.returns(T.all(T::Array[LanguageServer::Protocol::Interface::DocumentLink], Object)) } | ||
def run | ||
visit(@document.tree) | ||
@links | ||
end | ||
|
||
sig { params(node: SyntaxTree::Comment).void } | ||
def visit_comment(node) | ||
match = node.value.match(%r{source://(?<path>.*):(?<line>\d+)$}) | ||
return unless match | ||
|
||
file_path = if match[:path].start_with?(RUBY_ROOT) | ||
match[:path].sub(RUBY_ROOT, RbConfig::CONFIG["rubylibdir"]) | ||
else | ||
File.join(Bundler.bundle_path, "gems", match[:path]) | ||
end | ||
return unless File.exist?(file_path) | ||
|
||
target = "file://#{file_path}##{match[:line]}" | ||
|
||
@links << LanguageServer::Protocol::Interface::DocumentLink.new( | ||
range: range_from_syntax_tree_node(node), | ||
target: target, | ||
tooltip: "Jump to #{target.delete_prefix("file://")}" | ||
) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# typed: true | ||
|
||
class ExpectationsTestRunner | ||
sig { params(source: String).returns(Object) } | ||
def run_expectations(source); end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"result": [ | ||
{ | ||
"range": {"start": {"line": 0, "character": 0}, "end": {"line": 0, "character": 50}}, | ||
"target": "file://BUNDLER_PATH/gems/syntax_tree-SYNTAX_TREE_VERSION/lib/syntax_tree.rb#39", | ||
"tooltip": "Jump to BUNDLER_PATH/gems/syntax_tree-SYNTAX_TREE_VERSION/lib/syntax_tree.rb#39" | ||
}, | ||
{ | ||
"range": {"start": {"line": 4, "character": 0}, "end": {"line": 4, "character": 33}}, | ||
"target": "file://RUBY_ROOT/mutex_m.rb#1", | ||
"tooltip": "Jump to RUBY_ROOT/mutex_m.rb#1" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# source://syntax_tree-SYNTAX_TREE_VERSION/lib/syntax_tree.rb:39 | ||
def foo | ||
end | ||
|
||
# source://RUBY_ROOT/mutex_m.rb:1 | ||
def bar | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
require "expectations/expectations_test_runner" | ||
|
||
class DocumentLinkExpectationsTest < ExpectationsTestRunner | ||
expectations_tests RubyLsp::Requests::DocumentLink, "document_link" | ||
|
||
def assert_expectations(source, expected) | ||
source = substitute_syntax_tree_version(source) | ||
actual = T.cast(run_expectations(source), T::Array[LanguageServer::Protocol::Interface::DocumentLink]) | ||
assert_equal(map_expectations(json_expectations(expected)), JSON.parse(actual.to_json)) | ||
end | ||
|
||
def map_expectations(expectations) | ||
expectations.each do |expectation| | ||
expectation["target"] = substitute(expectation["target"]) | ||
expectation["tooltip"] = substitute(expectation["tooltip"]) | ||
end | ||
end | ||
|
||
def substitute(original) | ||
substitute_syntax_tree_version(original) | ||
.sub("BUNDLER_PATH", Bundler.bundle_path.to_s) | ||
.sub("RUBY_ROOT", RbConfig::CONFIG["rubylibdir"]) | ||
end | ||
|
||
def substitute_syntax_tree_version(original) | ||
original.sub("SYNTAX_TREE_VERSION", Gem::Specification.find_by_name("syntax_tree").version.to_s) | ||
end | ||
end |