Skip to content

Commit

Permalink
Disable diagnostics on large files
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Aug 23, 2024
1 parent 697b32a commit dfe3aca
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
5 changes: 4 additions & 1 deletion lib/ruby_lsp/requests/diagnostics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ def perform
diagnostics.concat(syntax_error_diagnostics, syntax_warning_diagnostics)

# Running RuboCop is slow, so to avoid excessive runs we only do so if the file is syntactically valid
return diagnostics if @document.syntax_error? || @active_linters.empty?
if @document.syntax_error? || @active_linters.empty? ||
@document.source.length > MAXIMUM_CHARACTERS_FOR_EXPENSIVE_FEATURES
return diagnostics
end

@active_linters.each do |linter|
linter_diagnostics = linter.run_diagnostic(@uri, @document)
Expand Down
4 changes: 0 additions & 4 deletions lib/ruby_lsp/requests/semantic_highlighting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ module Requests
class SemanticHighlighting < Request
extend T::Sig

# This maximum number of characters for providing highlighting is the same limit imposed by the VS Code TypeScript
# extension. Even with delta requests, anything above this number lags the editor significantly
MAXIMUM_CHARACTERS_FOR_HIGHLIGHT = 100_000

class << self
extend T::Sig

Expand Down
11 changes: 6 additions & 5 deletions lib/ruby_lsp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,14 @@ def text_document_did_open(message)
language_id: language_id,
)

if document.source.length > Requests::SemanticHighlighting::MAXIMUM_CHARACTERS_FOR_HIGHLIGHT
if document.source.length > MAXIMUM_CHARACTERS_FOR_EXPENSIVE_FEATURES
send_message(
Notification.new(
method: "window/showMessage",
params: Interface::ShowMessageParams.new(
type: Constant::MessageType::WARNING,
message: "This file is too long. For performance reasons, semantic highlighting will be disabled",
message: "This file is too long. For performance reasons, semantic highlighting and " \
"diagnostics will be disabled",
),
),
)
Expand Down Expand Up @@ -427,7 +428,7 @@ def run_combined_requests(message)
def text_document_semantic_tokens_full(message)
document = @store.get(message.dig(:params, :textDocument, :uri))

if document.source.length > Requests::SemanticHighlighting::MAXIMUM_CHARACTERS_FOR_HIGHLIGHT
if document.source.length > MAXIMUM_CHARACTERS_FOR_EXPENSIVE_FEATURES
send_empty_response(message[:id])
return
end
Expand All @@ -448,7 +449,7 @@ def text_document_semantic_tokens_full(message)
def text_document_semantic_tokens_delta(message)
document = @store.get(message.dig(:params, :textDocument, :uri))

if document.source.length > Requests::SemanticHighlighting::MAXIMUM_CHARACTERS_FOR_HIGHLIGHT
if document.source.length > MAXIMUM_CHARACTERS_FOR_EXPENSIVE_FEATURES
send_empty_response(message[:id])
return
end
Expand Down Expand Up @@ -476,7 +477,7 @@ def text_document_semantic_tokens_range(message)
uri = params.dig(:textDocument, :uri)
document = @store.get(uri)

if document.source.length > Requests::SemanticHighlighting::MAXIMUM_CHARACTERS_FOR_HIGHLIGHT
if document.source.length > MAXIMUM_CHARACTERS_FOR_EXPENSIVE_FEATURES
send_empty_response(message[:id])
return
end
Expand Down
4 changes: 4 additions & 0 deletions lib/ruby_lsp/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ module RubyLsp
)
GUESSED_TYPES_URL = "https://github.com/Shopify/ruby-lsp/blob/main/DESIGN_AND_ROADMAP.md#guessed-types"

# This maximum number of characters for providing expensive features, like semantic highlighting and diagnostics. This
# is the same number used by the TypeScript extension in VS Code
MAXIMUM_CHARACTERS_FOR_EXPENSIVE_FEATURES = 100_000

# A notification to be sent to the client
class Message
extend T::Sig
Expand Down

0 comments on commit dfe3aca

Please sign in to comment.