Skip to content

Commit

Permalink
Merge pull request #868 from Shopify/andyw8/migrate-diagnostics-to-yarp
Browse files Browse the repository at this point in the history
Migrate diagnostics to YARP
  • Loading branch information
andyw8 authored Aug 14, 2023
2 parents 578f965 + 289abae commit 183ef0a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
1 change: 0 additions & 1 deletion lib/ruby_lsp/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def initialize(source:, version:, uri:, encoding: Constant::PositionEncodingKind
@version = T.let(version, Integer)
@uri = T.let(uri, URI::Generic)
@unparsed_edits = T.let([], T::Array[EditShape])
@syntax_error = T.let(false, T::Boolean)
@parse_result = T.let(YARP.parse(@source), YARP::ParseResult)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ def diagnostic(uri)
Requests::Diagnostics.new(document).run
end

Interface::FullDocumentDiagnosticReport.new(kind: "full", items: response.map(&:to_lsp_diagnostic)) if response
Interface::FullDocumentDiagnosticReport.new(kind: "full", items: response) if response
end

sig { params(uri: URI::Generic, range: Document::RangeShape).returns(Interface::SemanticTokens) }
Expand Down
29 changes: 26 additions & 3 deletions lib/ruby_lsp/requests/diagnostics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,41 @@ def initialize(document)
@uri = T.let(document.uri, URI::Generic)
end

sig { override.returns(T.nilable(T.all(T::Array[Support::RuboCopDiagnostic], Object))) }
sig { override.returns(T.nilable(T.all(T::Array[Interface::Diagnostic], Object))) }
def run
# Running RuboCop is slow, so to avoid excessive runs we only do so if the file is syntactically valid
return if @document.syntax_error?
return syntax_error_diagnostics if @document.syntax_error?

return unless defined?(Support::RuboCopDiagnosticsRunner)

# Don't try to run RuboCop diagnostics for files outside the current working directory
path = @uri.to_standardized_path
return unless path.nil? || path.start_with?(T.must(WORKSPACE_URI.to_standardized_path))

Support::RuboCopDiagnosticsRunner.instance.run(@uri, @document)
Support::RuboCopDiagnosticsRunner.instance.run(@uri, @document).map!(&:to_lsp_diagnostic)
end

private

sig { returns(T.nilable(T::Array[Interface::Diagnostic])) }
def syntax_error_diagnostics
@document.parse_result.errors.map do |error|
Interface::Diagnostic.new(
range: Interface::Range.new(
start: Interface::Position.new(
line: error.location.start_line - 1,
character: error.location.start_column,
),
end: Interface::Position.new(
line: error.location.end_line - 1,
character: error.location.end_column,
),
),
message: error.message,
severity: Constant::DiagnosticSeverity::ERROR,
source: "YARP",
)
end
end
end
end
Expand Down
4 changes: 1 addition & 3 deletions test/requests/diagnostics_expectations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ def run_expectations(source)
end

assert_empty(stdout)

# On Windows, RuboCop will complain that the file is missing a carriage return at the end. We need to ignore these
T.must(result).map(&:to_lsp_diagnostic).reject { |diagnostic| diagnostic.code == "Layout/EndOfLine" }
T.must(result)
end

def assert_expectations(source, expected)
Expand Down
11 changes: 11 additions & 0 deletions test/requests/diagnostics_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,15 @@ def foo

assert_nil(RubyLsp::Requests::Diagnostics.new(document).run)
end

def test_returns_syntax_error_diagnostics
document = RubyLsp::Document.new(source: <<~RUBY, version: 1, uri: URI("file:///fake/file.rb"))
def foo
RUBY

diagnostics = T.must(RubyLsp::Requests::Diagnostics.new(document).run)

assert_equal(2, diagnostics.length)
assert_equal("Expected `end` to close `def` statement.", T.must(diagnostics.last).message)
end
end

0 comments on commit 183ef0a

Please sign in to comment.