Skip to content

Conversation

@tmtm
Copy link
Contributor

@tmtm tmtm commented Aug 11, 2025

Implemented support for TextDocumentSyncKind.Incremental in LSP, so that only text diffs are sent when changes are made in the editor.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@tmtm tmtm force-pushed the support-TextDocumentSyncKind.Incremental branch from 1beee8c to 067216e Compare August 11, 2025 05:38
@@ -0,0 +1 @@
* Support LSP TextDocumentSyncKind.Incremental. ([@tmtm][])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update this changelog entry to the following and squash your commits into one?

Suggested change
* Support LSP TextDocumentSyncKind.Incremental. ([@tmtm][])
* [#14431](https://github.com/rubocop/rubocop/pull/14431): Support LSP `TextDocumentSyncKind.Incremental`. ([@tmtm][])

@tmtm tmtm force-pushed the support-TextDocumentSyncKind.Incremental branch 2 times, most recently from d9145f3 to e188292 Compare August 11, 2025 07:29
@koic
Copy link
Member

koic commented Aug 11, 2025

Please don't split the changes into separate commits for the new change entry. Can you squash them into a single commit?

@tmtm tmtm force-pushed the support-TextDocumentSyncKind.Incremental branch from e188292 to 37b7d8c Compare August 11, 2025 09:19
@tmtm tmtm marked this pull request as draft August 11, 2025 13:19
@tmtm tmtm force-pushed the support-TextDocumentSyncKind.Incremental branch from 37b7d8c to 620921b Compare August 11, 2025 13:23
@tmtm tmtm marked this pull request as ready for review August 11, 2025 13:33
@tmtm tmtm marked this pull request as draft August 11, 2025 14:00
@tmtm tmtm marked this pull request as ready for review August 11, 2025 14:49
@koic
Copy link
Member

koic commented Aug 11, 2025

Thank you. Let's proceed with this.

@koic koic merged commit 50618e8 into rubocop:master Aug 11, 2025
23 checks passed
@koic
Copy link
Member

koic commented Aug 13, 2025

@tmtm Unfortunately, this change is insufficient and, for example, the following situation causes problems.
The LSP client environment is Eglot 1.17.

  1. Open spec/rubocop/cop/style/dir_spec.rb and insert byebug at L16:
 # frozen_string_literal: true                                                                                                                                                                

 RSpec.describe RuboCop::Cop::Style::Dir, :config do
   context 'when using `#expand_path` and `#dirname`' do
     it 'registers an offense' do
       expect_offense(<<~RUBY)                                                                                                                                                                
         File.expand_path(File.dirname(__FILE__))                                                                                                                                             
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `__dir__` to get an absolute path to the current file's directory.                                                                      
       RUBY                                                                                                                                                                                   

       expect_correction(<<~RUBY)                                                                                                                                                             
         __dir__                                                                                                                                                                              
       RUBY                                                                                                                                                                                   
     end

+    byebug
     
     it 'registers an offense with ::File' do
       expect_offense(<<~RUBY)                                                                                                                                                                
         ::File.expand_path(::File.dirname(__FILE__))                                                                                                                                         
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `__dir__` to get an absolute path to the current file's directory.                                                                  
       RUBY                                                                                                                                                                                   

       expect_correction(<<~RUBY)                                                                                                                                                             
         __dir__                                                                                                                                                                              
       RUBY                                                                                                                                                                                   
     end
   end
  1. Run autocorrect on save. This results in unexpected changes to the code, such as it becoming corrupted to something like it ug. The way the code gets corrupted seems to be random.
# frozen_string_literal: true                                                                                                                                                                

 RSpec.describe RuboCop::Cop::Style::Dir, :config do
   context 'when using `#expand_path` and `#dirname`' do
     it 'registers an offense' do
       expect_offense(<<~RUBY)                                                                                                                                                                
         File.expand_path(File.dirname(__FILE__))                                                                                                                                             
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `__dir__` to get an absolute path to the current file's directory.                                                                      
       RUBY                                                                                                                                                                                   

       expect_correction(<<~RUBY)                                                                                                                                                             
         __dir__                                                                                                                                                                              
       RUBY                                                                                                                                                                                   
     end

+    byebug
+
+    it ug 'registers an offense with ::File' do
       expect_offense(<<~RUBY)                                                                                                                                                                
         ::File.expand_path(::File.dirname(__FILE__))                                                                                                                                         
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `__dir__` to get an absolute path to the current file's directory.                                                                  
       RUBY                                                                                                                                                                                   

       expect_correction(<<~RUBY)                                                                                                                                                             
         __dir__                                                                                                                                                                              
       RUBY                                                                                                                                                                                   
     end
   end

Is there a plan to fix this bug promptly?
If not, I will revert this PR once for LSP stability.

@tmtm
Copy link
Contributor Author

tmtm commented Aug 13, 2025

@koic I’m not sure what the cause is. I couldn’t reproduce it in my environment. 😢

@tmtm
Copy link
Contributor Author

tmtm commented Aug 13, 2025

Ah, I understood! When making quick edits at multiple positions in Emacs, contentChanges becomes an array with multiple elements, but I was only processing the first one!

@tmtm
Copy link
Contributor Author

tmtm commented Aug 13, 2025

@koic However, I won’t have time to fix it immediately, so please revert it.

koic added a commit to koic/rubocop that referenced this pull request Aug 13, 2025
…on the LSP

Follow-up to rubocop#14431 (comment)

This PR fixes an unexpect diagnostic with `TextDocumentSyncKind.Incremental` on the LSP
when multiple `contentChanges`.

Since rubocop#14431 changed `TextDocumentSyncKind` from `Full` to `Incremental`,
it is necessary to handle the possibility that `params[:contentChanges]` may contain multiple entries.
@koic
Copy link
Member

koic commented Aug 13, 2025

@tmtm Thank you for investigating! I’ve opened #14442. I believe this should fix the issue.

koic added a commit to koic/rubocop that referenced this pull request Aug 13, 2025
…on the LSP

Follow-up to rubocop#14431 (comment)

This PR fixes an unexpect diagnostic with `TextDocumentSyncKind.Incremental` on the LSP
when multiple `contentChanges`.

Since rubocop#14431 changed `TextDocumentSyncKind` from `Full` to `Incremental`,
it is necessary to handle the possibility that `params[:contentChanges]` may contain multiple entries.

Ref: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_synchronization
koic added a commit to koic/rubocop that referenced this pull request Aug 13, 2025
…on the LSP

Follow-up to rubocop#14431 (comment)

This PR fixes an unexpect diagnostic with `TextDocumentSyncKind.Incremental` on the LSP
when `contentChanges` contains multiple entries.

Since rubocop#14431 changed `TextDocumentSyncKind` from `Full` to `Incremental`,
it is necessary to handle the possibility that `params[:contentChanges]` may contain multiple entries.

Ref: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_synchronization
bbatsov pushed a commit that referenced this pull request Aug 14, 2025
…on the LSP

Follow-up to #14431 (comment)

This PR fixes an unexpect diagnostic with `TextDocumentSyncKind.Incremental` on the LSP
when `contentChanges` contains multiple entries.

Since #14431 changed `TextDocumentSyncKind` from `Full` to `Incremental`,
it is necessary to handle the possibility that `params[:contentChanges]` may contain multiple entries.

Ref: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_synchronization
@koic koic mentioned this pull request Aug 17, 2025
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants