Skip to content
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

Disable diagnostics on large files #2483

Merged
merged 1 commit into from
Aug 23, 2024
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
9 changes: 9 additions & 0 deletions lib/ruby_lsp/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class LanguageId < T::Enum
extend T::Generic

ParseResultType = type_member

# 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
EMPTY_CACHE = T.let(Object.new.freeze, Object)

abstract!
Expand Down Expand Up @@ -113,6 +117,11 @@ def create_scanner
Scanner.new(@source, @encoding)
end

sig { returns(T::Boolean) }
def past_expensive_limit?
@source.length > MAXIMUM_CHARACTERS_FOR_EXPENSIVE_FEATURES
end

class Scanner
extend T::Sig

Expand Down
4 changes: 3 additions & 1 deletion lib/ruby_lsp/requests/diagnostics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ 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.past_expensive_limit?
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.past_expensive_limit?
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 " \
vinistock marked this conversation as resolved.
Show resolved Hide resolved
"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.past_expensive_limit?
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.past_expensive_limit?
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.past_expensive_limit?
send_empty_response(message[:id])
return
end
Expand Down
Loading