diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index 8651c46c..19b24e85 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -1,359 +1,146 @@ -# Attribution - -This project includes code from or is derived from the following open source projects. We are grateful to the authors and contributors of these projects. - ---- - -## Bundled Dependencies - -### utf8proc - -**License**: MIT License -**Copyright**: Copyright (c) 2014-2021 Steven G. Johnson, Jiahao Chen, Tony Kelman, Jonas Fonseca, and other contributors -**Source**: https://github.com/JuliaStrings/utf8proc -**Location**: `libvscode-diff/vendor/` -**Purpose**: UTF-8 Unicode string processing - -Full license text: [libvscode-diff/vendor/utf8proc_LICENSE.md](libvscode-diff/vendor/utf8proc_LICENSE.md) - ---- - -## Derivative Works - -### Microsoft Visual Studio Code - -**License**: MIT License -**Copyright**: Copyright (c) Microsoft Corporation -**Source**: https://github.com/microsoft/vscode -**Description**: The diff computation algorithm in this project is a C port of VSCode's `defaultLinesDiffComputer` implementation. The algorithm, data structures, and optimization heuristics are derived from VSCode's TypeScript source code. - -**Key Components Ported**: -- Myers diff algorithm (`src/vs/editor/common/diff/defaultLinesDiffComputer/algorithms/myersDiffAlgorithm.ts`) -- Dynamic Programming algorithm (`src/vs/editor/common/diff/defaultLinesDiffComputer/algorithms/dynamicProgrammingDiffing.ts`) -- Line-level optimization heuristics (`src/vs/editor/common/diff/defaultLinesDiffComputer/heuristicSequenceOptimizations.ts`) -- Character-level refinement (`src/vs/editor/common/diff/defaultLinesDiffComputer/defaultLinesDiffComputer.ts`) -- Range mapping data structures (`src/vs/editor/common/diff/rangeMapping.ts`) - -**VSCode License**: MIT License (see [official license](https://github.com/microsoft/vscode/blob/main/LICENSE.txt)) - -``` -MIT License - -Copyright (c) Microsoft Corporation - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -``` - ---- - -## Vendored Code - -### Neovim LSP Semantic Tokens - -**License**: Apache License 2.0 -**Copyright**: Copyright Neovim contributors -**Source**: https://github.com/neovim/neovim -**Location**: `lua/vscode-diff/render/semantic_tokens.lua` (lines 38-117) -**Description**: Two helper functions from Neovim's LSP semantic tokens implementation are vendored because there is no public API to process semantic token responses for arbitrary buffers. - -**Functions Vendored**: -- `modifiers_from_number()` - Decodes token modifiers from bit field -- `tokens_to_ranges()` - Converts LSP token array to highlight ranges - -**Reason for Vendoring**: Neovim's LSP semantic token API is designed for regular buffers only. Virtual/scratch buffers need direct access to the parsing functions, which are not publicly exported. - -**Neovim License**: Apache License 2.0 (see [official license](https://github.com/neovim/neovim/blob/master/LICENSE.txt)) - -``` - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - Copyright Neovim contributors. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -``` - ---- - -## External Dependencies - -The following dependencies are not bundled but are required for full functionality: - -### nui.nvim - -**License**: MIT License -**Author**: Munif Tanjim -**Source**: https://github.com/MunifTanjim/nui.nvim -**Purpose**: UI components for file explorer - ---- - -## Architectural Inspiration - -The following projects inspired architectural decisions but no code was copied: - -### vim-fugitive - -**Author**: Tim Pope -**Source**: https://github.com/tpope/vim-fugitive -**Inspiration**: The virtual file URL scheme (`codediff://`) is inspired by vim-fugitive's `fugitive://` pattern for creating virtual buffers that represent git objects. - -### gitsigns.nvim & diffview.nvim - -**Sources**: -- https://github.com/lewis6991/gitsigns.nvim -- https://github.com/sindrets/diffview.nvim - -**Inspiration**: Async git integration patterns and best practices for Neovim git plugins. - ---- - -## Documentation Assets - -Colorschemes used in documentation screenshots: - -### Tokyo Night - -**Author**: folke -**License**: Apache License 2.0 -**Source**: https://github.com/folke/tokyonight.nvim -**Usage**: Hero image (Tokyo Night Moon variant) - -### Dawnfox - -**Author**: EdenEast -**License**: MIT License -**Source**: https://github.com/EdenEast/nightfox.nvim -**Usage**: Highlight groups visual example (Dawnfox Light variant) - -### Catppuccin - -**Author**: Catppuccin Community -**License**: MIT License -**Source**: https://github.com/catppuccin/nvim -**Usage**: Highlight groups visual example (Mocha variant) - -### Kanagawa - -**Author**: rebelot -**License**: MIT License -**Source**: https://github.com/rebelot/kanagawa.nvim -**Usage**: Highlight groups visual example (Lotus variant) - ---- - -## Acknowledgments - -We would like to thank: - -- **Microsoft Corporation** and the VSCode team for creating and open-sourcing an excellent diff algorithm implementation -- **The Neovim contributors** for LSP infrastructure and semantic token support -- **The JuliaStrings project** and utf8proc contributors for providing a robust Unicode processing library -- **Tim Pope** (vim-fugitive) for pioneering the virtual file URL pattern -- **The Neovim community** for creating the plugin ecosystem and supporting libraries -- **Colorscheme authors** (folke, EdenEast, Catppuccin Community, rebelot) for their beautiful themes used in our documentation -- All contributors to the dependencies and inspirations listed above - ---- - -*This project is distributed under the MIT License. See LICENSE file for details.* +# Attribution + +This project includes code from or is derived from the following open source projects. We are grateful to the authors and contributors of these projects. + +--- + +## Bundled Dependencies + +### utf8proc + +**License**: MIT License +**Copyright**: Copyright (c) 2014-2021 Steven G. Johnson, Jiahao Chen, Tony Kelman, Jonas Fonseca, and other contributors +**Source**: https://github.com/JuliaStrings/utf8proc +**Location**: `libvscode-diff/vendor/` +**Purpose**: UTF-8 Unicode string processing + +Full license text: [libvscode-diff/vendor/utf8proc_LICENSE.md](libvscode-diff/vendor/utf8proc_LICENSE.md) + +--- + +## Derivative Works + +### Microsoft Visual Studio Code + +**License**: MIT License +**Copyright**: Copyright (c) Microsoft Corporation +**Source**: https://github.com/microsoft/vscode +**Description**: The diff computation algorithm in this project is a C port of VSCode's `defaultLinesDiffComputer` implementation. The algorithm, data structures, and optimization heuristics are derived from VSCode's TypeScript source code. + +**Key Components Ported**: +- Myers diff algorithm (`src/vs/editor/common/diff/defaultLinesDiffComputer/algorithms/myersDiffAlgorithm.ts`) +- Dynamic Programming algorithm (`src/vs/editor/common/diff/defaultLinesDiffComputer/algorithms/dynamicProgrammingDiffing.ts`) +- Line-level optimization heuristics (`src/vs/editor/common/diff/defaultLinesDiffComputer/heuristicSequenceOptimizations.ts`) +- Character-level refinement (`src/vs/editor/common/diff/defaultLinesDiffComputer/defaultLinesDiffComputer.ts`) +- Range mapping data structures (`src/vs/editor/common/diff/rangeMapping.ts`) + +**VSCode License**: MIT License (see [official license](https://github.com/microsoft/vscode/blob/main/LICENSE.txt)) + +``` +MIT License + +Copyright (c) Microsoft Corporation + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +``` + +--- + +## External Dependencies + +The following dependencies are not bundled but are required for full functionality: + +### nui.nvim + +**License**: MIT License +**Author**: Munif Tanjim +**Source**: https://github.com/MunifTanjim/nui.nvim +**Purpose**: UI components for file explorer + +--- + +## Architectural Inspiration + +The following projects inspired architectural decisions but no code was copied: + +### vim-fugitive + +**Author**: Tim Pope +**Source**: https://github.com/tpope/vim-fugitive +**Inspiration**: The virtual file URL scheme (`codediff://`) is inspired by vim-fugitive's `fugitive://` pattern for creating virtual buffers that represent git objects. + +### gitsigns.nvim & diffview.nvim + +**Sources**: +- https://github.com/lewis6991/gitsigns.nvim +- https://github.com/sindrets/diffview.nvim + +**Inspiration**: Async git integration patterns and best practices for Neovim git plugins. + +--- + +## Documentation Assets + +Colorschemes used in documentation screenshots: + +### Tokyo Night + +**Author**: folke +**License**: Apache License 2.0 +**Source**: https://github.com/folke/tokyonight.nvim +**Usage**: Hero image (Tokyo Night Moon variant) + +### Dawnfox + +**Author**: EdenEast +**License**: MIT License +**Source**: https://github.com/EdenEast/nightfox.nvim +**Usage**: Highlight groups visual example (Dawnfox Light variant) + +### Catppuccin + +**Author**: Catppuccin Community +**License**: MIT License +**Source**: https://github.com/catppuccin/nvim +**Usage**: Highlight groups visual example (Mocha variant) + +### Kanagawa + +**Author**: rebelot +**License**: MIT License +**Source**: https://github.com/rebelot/kanagawa.nvim +**Usage**: Highlight groups visual example (Lotus variant) + +--- + +## Acknowledgments + +We would like to thank: + +- **Microsoft Corporation** and the VSCode team for creating and open-sourcing an excellent diff algorithm implementation +- **The Neovim contributors** for the editor and plugin infrastructure +- **The JuliaStrings project** and utf8proc contributors for providing a robust Unicode processing library +- **Tim Pope** (vim-fugitive) for pioneering the virtual file URL pattern +- **The Neovim community** for creating the plugin ecosystem and supporting libraries +- **Colorscheme authors** (folke, EdenEast, Catppuccin Community, rebelot) for their beautiful themes used in our documentation +- All contributors to the dependencies and inspirations listed above + +--- + +*This project is distributed under the MIT License. See LICENSE file for details.* diff --git a/README.md b/README.md index 1d0feabd..eefcd392 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ https://github.com/user-attachments/assets/3c66a26d-5ff9-4dac-8035-a2f2b7bd2308 - **Precise diffs:** See line and character changes in side-by-side or inline layouts. - **Focused navigation:** Move between files and hunks, fold unchanged code, and track moved blocks. - **Conflict resolution:** Resolve merge conflicts per block or across the whole file. -- **Editor-native context:** Keep Tree-sitter and semantic highlighting in revision buffers. +- **Editor-native context:** Keep Tree-sitter syntax highlighting in revision buffers. ## Installation diff --git a/docs/development/04-lua-features/virtual-file-implementation.md b/docs/development/04-lua-features/virtual-file-implementation.md index ddf53346..8debdd6c 100644 --- a/docs/development/04-lua-features/virtual-file-implementation.md +++ b/docs/development/04-lua-features/virtual-file-implementation.md @@ -1,429 +1,66 @@ -# Virtual File Implementation for LSP Semantic Tokens +# Virtual File Implementation -## ✅ COMPLETE - Matching vim-fugitive's Architecture +## Purpose -### What We Built +CodeDiff uses a `codediff://` URI for read-only content loaded from a Git revision. +The URI gives each historical buffer a stable identity while keeping it separate +from the real working-tree file. -A **virtual file URL scheme** (`codediff://`) that allows LSP servers to attach to and analyze git historical content, providing accurate semantic token highlighting. - ---- - -## Buffer Type Architecture (Refactored) - -The implementation now uses an **explicit, flexible buffer type system** that supports all combinations of buffer types: - -### Buffer Type Enum +## Buffer Types ```lua BufferType = { - VIRTUAL_FILE = "VIRTUAL_FILE", -- Virtual file (codediff://) for LSP semantic tokens - REAL_FILE = "REAL_FILE", -- Real file on disk - SCRATCH = "SCRATCH" -- Scratch buffer with no file backing + VIRTUAL_FILE = "VIRTUAL_FILE", -- read-only content from a Git revision + REAL_FILE = "REAL_FILE", -- a file on disk + SCRATCH = "SCRATCH", -- an in-memory comparison buffer } ``` -### Supported Combinations - -The system is **generalized** and supports all 4 combinations: - -| Left Buffer | Right Buffer | Use Case | -|----------------|----------------|---------------------------------------------| -| VIRTUAL_FILE | REAL_FILE | **Git diff** (current implementation) | -| SCRATCH | SCRATCH | **File-to-file diff** (current) | -| VIRTUAL_FILE | VIRTUAL_FILE | Future: Diff two git revisions | -| REAL_FILE | REAL_FILE | Future: Diff two open buffers | - -### Design Principles - -1. **View.lua is Agnostic**: Doesn't make assumptions about which buffer is virtual/real -2. **Upstream Decides**: `commands.lua` determines buffer types based on user command -3. **Explicit Parameters**: Buffer types and configs passed explicitly via `opts` -4. **Flexible**: Easy to add new buffer type combinations in the future - ---- - -## Current Buffer Modes - -### 1. Git Diff Mode (VIRTUAL_FILE + REAL_FILE) -- **Used for:** `:CodeDiff HEAD` (comparing working file with git revision) -- **Left buffer:** Virtual file with `codediff://` URL -- **Right buffer:** Real file buffer (working file) -- **LSP attachment:** Left buffer gets LSP via virtual file URL scheme -- **Semantic tokens:** Requested from right buffer's LSP server for left buffer content -- **Lifecycle:** Virtual buffer cleaned up when tab closes - -### 2. File-to-File Diff Mode (SCRATCH + SCRATCH) -- **Used for:** `:CodeDiff file1 file2` (comparing two actual files) -- **Left buffer:** Scratch buffer with content from first file -- **Right buffer:** Scratch buffer with content from second file -- **LSP attachment:** None (both are scratch buffers) -- **Semantic tokens:** None -- **Lifecycle:** Scratch buffers wiped when tab closes - ---- - -## Architecture Overview - -### Inspired by vim-fugitive - -Vim-fugitive uses `fugitive://` URLs to create "real" file buffers that LSP can attach to. We implemented the same pattern for git diffs: - -**URL Format:** -``` -codediff:///path/to/git-root///commit-hash/relative/path/to/file.lua -``` - -**Example:** -``` -codediff:////home/user/project///HEAD/src/file.lua -``` - ---- - -## Implementation Details - -### 1. Virtual File Module (`lua/vscode-diff/virtual_file.lua`) - -**Purpose:** Handle virtual file URL scheme for git revisions - -**Key Functions:** -- `create_url(git_root, commit, filepath)` - Generate codediff:// URL -- `parse_url(url)` - Parse URL back to components -- `setup()` - Register BufReadCmd and BufWriteCmd autocmds - -**How It Works:** -1. **BufReadCmd Autocmd** intercepts reads of `codediff://` URLs -2. **git.get_file_at_revision()** fetches content from git -3. **Populates Buffer** with historical content -4. **Sets Filetype** for TreeSitter and LSP -5. **Triggers BufRead** event for LSP attachment -6. **Fires Custom Event** (`CodeDiffVirtualFileLoaded`) for diff highlighting - -```lua --- BufReadCmd callback pseudocode: -1. Parse codediff:// URL -2. Call git.get_file_at_revision(commit, filepath, callback) -3. In callback: - - Set buffer lines - - Mark buffer as readonly - - Detect and set filetype - - Fire CodeDiffVirtualFileLoaded event - - Fire BufRead event (for LSP) -``` - ---- - -### 2. Updated View Creation (`lua/vscode-diff/render/view.lua`) - -**Changes:** -- Detect if this is a git diff (has `git_revision` and `git_root`) -- For git diffs: Create virtual file URL buffer instead of scratch buffer -- Skip setting content (BufReadCmd handles it) -- Listen for `CodeDiffVirtualFileLoaded` event -- Apply diff highlights AFTER virtual file loads - -**Flow for Virtual Files:** -```lua -1. Create buffer with codediff:// URL (vim.fn.bufadd) -2. Create windows and display buffer - └─> This triggers BufReadCmd -3. BufReadCmd loads content asynchronously -4. Fires CodeDiffVirtualFileLoaded event -5. Event handler applies: - - Diff highlights - - Semantic tokens - - Auto-scroll to first hunk -``` - -**Flow for Non-Virtual Files (unchanged):** -```lua -1. Create scratch buffer -2. Set content immediately -3. Apply diff highlights immediately -4. Apply semantic tokens immediately -``` - ---- - -### 3. Updated Commands (`lua/vscode-diff/commands.lua`) - -**Changes:** -- Pass `git_revision` and `git_root` to `create_diff_view()` -- These trigger virtual file creation - -```lua -render.create_diff_view(lines_git, lines_current, lines_diff, { - right_file = current_file, - git_revision = revision, -- NEW - git_root = git_root, -- NEW -}) -``` - ---- - -### 4. Semantic Tokens (`lua/vscode-diff/render/semantic.lua`) - -**Removed:** Content matching check (no longer needed!) - -**Before:** -```lua --- Check if buffers have same content -if content_differs then - return false -- Skip semantic tokens -end -``` - -**After:** -```lua --- With virtual files, LSP analyzes the correct content --- No content check needed! -``` - -**Why This Works:** -- Virtual file has its own URI (`codediff://...`) -- LSP server sees it as a separate file -- Analyzes the buffer's actual content (git historical version) -- Tokens are accurate for that version! - ---- - -## Benefits - -### ✅ Accurate Semantic Highlighting +The normal Git diff view combines a virtual original buffer with a real modified +buffer. File-to-file comparisons use scratch buffers. -**Before (scratch buffers):** -- LSP analyzed current file -- Applied tokens to historical content -- **Result:** Misaligned, wrong highlights +## Loading a Virtual Buffer -**After (virtual files):** -- LSP analyzes virtual file's content -- Tokens match the historical version -- **Result:** Perfect highlighting! ✨ +The virtual-file loader: -### ✅ Full LSP Features +1. Parses the `codediff://` URI. +2. Reads the requested revision asynchronously through Git. +3. Places the content in the buffer. +4. Marks the buffer read-only and disables diagnostics. +5. Starts Tree-sitter directly when a parser is available. +6. Fires `CodeDiffVirtualFileLoaded` so the view can render the diff. -LSP servers can now attach to left buffer: -- ✅ Semantic tokens -- ✅ Hover information (if enabled) -- ✅ Go-to-definition (works within that version) -- ❌ Diagnostics (disabled - buffer is readonly) +Tree-sitter is started directly instead of assigning `filetype`. This prevents +`FileType` autocmds from causing LSP clients to attach to a URI scheme they may +not support. -### ✅ Matches vim-fugitive +## Highlighting and LSP Boundary -Our implementation follows the same proven architecture: -- Virtual URL scheme -- BufReadCmd handler -- Async content loading -- LSP-friendly buffers +Virtual revision buffers use Tree-sitter syntax highlighting. They are not LSP +client buffers and do not send document notifications to language servers. ---- +The real working-tree buffer remains a normal Neovim buffer, so users retain +normal LSP behavior when they leave the diff view or open the file separately. -## Testing +## Lifecycle -### Test Coverage +Virtual buffers are reused when possible, refreshed for mutable revisions such +as `:0`, and deleted when they are no longer needed. The view waits for +`CodeDiffVirtualFileLoaded` before reading content that was loaded asynchronously. -**11 semantic token tests** covering: -1. Module loading -2. Version compatibility -3. Bit operations -4. Buffer cleanup -5. Namespace creation -6. LSP integration -7. Token data structure -8. Priority settings -9. URI construction -10. Missing capabilities -11. **Virtual file URL creation/parsing** ← NEW +## URI Format -**All 34 integration tests pass:** -- 10 FFI tests -- 8 Git tests -- 5 Autoscroll tests -- 11 Semantic token tests - -### Validation - -Tested with headless Neovim: -```bash -✅ Virtual buffer loaded: true -✅ Content lines: 10 -✅ Filetype: lua -✅ Diff highlights: 1 -✅ Semantic token highlights: 6 ← THE MAGIC! -✅ LSP clients attached: 1 (lua_ls) +```text +codediff:///path/to/repository///revision/path/to/file ``` ---- - -## Files Modified/Created - -### New Files -1. `lua/vscode-diff/virtual_file.lua` (96 lines) - - Virtual file URL scheme implementation - -### Modified Files -1. `lua/vscode-diff/init.lua` - - Setup virtual_file.setup() - -2. `lua/vscode-diff/commands.lua` - - Pass git_revision and git_root - -3. `lua/vscode-diff/render/view.lua` - - Virtual file buffer creation - - Async highlight application - - Event-driven workflow - -4. `lua/vscode-diff/render/core.lua` - - Added skip_left_content parameter - -5. `lua/vscode-diff/render/semantic.lua` - - Removed content check - - Updated documentation - -6. `tests/render/test_semantic_tokens.lua` - - Updated test 11 to test virtual files - ---- - -## Performance - -**Virtual File Creation:** -- URL generation: < 1ms -- BufReadCmd trigger: < 1ms -- Git content fetch: 10-50ms (async) -- Total perceived latency: ~0ms (async!) - -**No Performance Impact:** -- Content loading is async -- Diff view opens immediately -- Highlights apply when ready - ---- - -## Edge Cases Handled - -✅ **Error Handling:** -- Invalid git revision → Error message in buffer -- Missing file → Error message in buffer -- LSP not available → Falls back to TreeSitter only - -✅ **Buffer Lifecycle:** -- Cleanup autocmds after highlights applied -- Proper readonly/modifiable settings -- No buffer leaks - -✅ **Window Management:** -- Cursor positioning for virtual files -- Auto-scroll after content loads -- Scrollbind works correctly - ---- - -## Future Improvements - -Potential enhancements: -1. Cache virtual file content to avoid repeated git calls -2. Support for staged changes (`:0:` index) -3. Support for working tree changes -4. Diff against arbitrary commits - ---- - -## Summary - -We successfully implemented a **production-ready virtual file system** that: - -- ✅ Matches vim-fugitive's proven architecture -- ✅ Enables accurate LSP semantic tokens for git history -- ✅ Maintains backward compatibility (scratch buffers still work) -- ✅ Passes all 34 integration tests -- ✅ Zero performance impact (fully async) -- ✅ Handles all edge cases - -**The implementation is COMPLETE and READY for production use!** 🎉 - ---- - -## December 2024 Refactoring: Generalized Buffer Type System - -### Motivation - -The original implementation had hardcoded assumptions about buffer types: -- Git diff mode → left is virtual file, right is real file -- File-to-file mode → both are scratch buffers - -This was inflexible and made the code harder to understand and extend. - -### Refactoring Goals - -1. **Make buffer types explicit and configurable** -2. **Support all 4 combinations of buffer types** (virtual/real/scratch for both left and right) -3. **Move decision logic upstream** to commands.lua (separation of concerns) -4. **Keep view.lua agnostic** - it shouldn't know or care about the use case - -### What Changed - -#### Before (Implicit Mode Detection) -```lua --- view.lua decided based on opts -local mode = determine_buffer_mode(opts) -- VIRTUAL_FILE or REAL_BUFFER -if mode == BUFFER_MODE.VIRTUAL_FILE then - -- Create virtual file for left, real file for right -else - -- Create scratch buffers -end -``` - -#### After (Explicit Buffer Types) -```lua --- commands.lua decides and passes explicit types -render.create_diff_view(lines_a, lines_b, diff, { - left_type = render.BufferType.VIRTUAL_FILE, - left_config = { git_root = ..., git_revision = ..., relative_path = ... }, - right_type = render.BufferType.REAL_FILE, - right_config = { file_path = ... }, - filetype = "lua", -}) -``` - -### Benefits - -1. **Flexibility**: Easy to add new combinations (e.g., diff two git revisions, both virtual files) -2. **Clarity**: Buffer types are explicit in the API, not inferred -3. **Modularity**: view.lua is purely a rendering layer, doesn't make business logic decisions -4. **Maintainability**: Adding new features doesn't require changing view.lua's logic - -### API Changes - -**New Public API:** -- `render.BufferType` enum exported -- `view.create()` now requires `left_type`, `right_type`, `left_config`, `right_config` - -**Backward Compatibility:** -- Old API is REMOVED (breaking change) -- All callers updated to new explicit API -- All tests updated and passing - -### Testing - -✅ All 34 existing integration tests pass -✅ Git diff with virtual files works correctly -✅ File-to-file diff with scratch buffers works correctly -✅ Buffer lifecycle management works for all types - ---- - -## Summary - -We successfully implemented a **production-ready virtual file system** that: +The URI is an internal buffer identity. It is not a filesystem path and should +not be passed to tools or language servers that only accept `file://` URIs. -- ✅ Matches vim-fugitive's proven architecture -- ✅ Enables accurate LSP semantic tokens for git history -- ✅ Maintains modular, extensible design -- ✅ Passes all 34 integration tests -- ✅ Zero performance impact (fully async) -- ✅ Handles all edge cases -- ✅ **Generalized buffer type system for future extensibility** +## Related Code -**The implementation is COMPLETE and READY for production use!** 🎉 +- `lua/codediff/core/virtual_file.lua` — URI parsing, loading, and Tree-sitter setup +- `lua/codediff/ui/view/render.lua` — side-by-side rendering after load +- `lua/codediff/ui/inline_view/render.lua` — inline rendering after load +- `tests/core/virtual_file_lsp_spec.lua` — regression coverage ensuring virtual + buffers do not trigger LSP attachment diff --git a/docs/development/05-architecture/architecture-refactor-plan.md b/docs/development/05-architecture/architecture-refactor-plan.md index 6fceae11..d4a466a6 100644 --- a/docs/development/05-architecture/architecture-refactor-plan.md +++ b/docs/development/05-architecture/architecture-refactor-plan.md @@ -37,7 +37,6 @@ This document proposes a comprehensive refactoring of the vscode-diff.nvim plugi 382 auto_refresh.lua ✅ Auto-refresh 353 commands.lua ✅ Command handlers 266 diff.lua ✅ FFI wrapper - 260 render/semantic_tokens.lua ✅ Semantic tokens 206 render/highlights.lua ✅ Highlight setup 162 virtual_file.lua ✅ Virtual files @@ -133,7 +132,6 @@ lua/vscode-diff/ │ ├── init.lua (18 lines - KEEP) │ ├── highlights.lua (206 lines - KEEP) │ ├── core.lua (519 lines - KEEP, diff rendering engine) -│ ├── semantic_tokens.lua (260 lines - KEEP) │ ├── merge_alignment.lua (462 lines - KEEP) │ ├── auto_refresh.lua (382 lines - MOVE from root) │ │ diff --git a/docs/development/README.md b/docs/development/README.md index 25f0d964..6b1ef610 100644 --- a/docs/development/README.md +++ b/docs/development/README.md @@ -36,7 +36,7 @@ Read everything in order to understand the complete development journey: 1. [04-lua-features/git-integration.md](04-lua-features/git-integration.md) — Git revision comparison 2. [04-lua-features/rendering-evolution.md](04-lua-features/rendering-evolution.md) — Filler lines, colors, extmark layering -3. [04-lua-features/virtual-file-implementation.md](04-lua-features/virtual-file-implementation.md) — LSP semantic tokens via virtual files +3. [04-lua-features/virtual-file-implementation.md](04-lua-features/virtual-file-implementation.md) — Tree-sitter highlighting via virtual files 4. [04-lua-features/explorer-mode.md](04-lua-features/explorer-mode.md) — File sidebar with git status 5. [05-architecture/auto-refresh-strategy.md](05-architecture/auto-refresh-strategy.md) — How VSCode refreshes diffs 6. [05-architecture/async-diff-architecture.md](05-architecture/async-diff-architecture.md) — Non-blocking diff computation @@ -75,7 +75,7 @@ docs/development/ | Oct 28-29, 2025 | VSCode extraction tool, UTF-8/UTF-16 comprehensive fixes | | Oct 26, 2025 | Cross-platform compatibility (Windows MSVC support) | | Oct 27, 2025 | Lua integration: git revision diff, rendering improvements | -| Nov 2025 | Virtual files, semantic tokens, auto-refresh, timeout fix, installer | +| Nov 2025 | Virtual files, Tree-sitter highlighting, auto-refresh, timeout fix, installer | | Nov 12, 2025 | Explorer mode with file sidebar | | Dec 2025 | Merge tool alignment parity, architecture refactor planning | diff --git a/lua/codediff/core/virtual_file.lua b/lua/codediff/core/virtual_file.lua index 7f6fb6fd..ec43deab 100644 --- a/lua/codediff/core/virtual_file.lua +++ b/lua/codediff/core/virtual_file.lua @@ -1,7 +1,6 @@ -- Virtual file scheme for git revisions -- Inspired by vim-fugitive's fugitive:// URL scheme --- LSP attachment is prevented via LspAttach guard; semantic tokens --- are handled separately via semantic_tokens.lua +-- LSP attachment is prevented so revision buffers use Tree-sitter highlighting only. local M = {} @@ -61,7 +60,7 @@ local function load_virtual_buffer_content(buf, git_root, commit, filepath) if ft then local lang = vim.treesitter.language.get_lang(ft) or ft if pcall(vim.treesitter.start, buf, lang) then - -- Store filetype for semantic_tokens without firing autocmds + -- Keep syntax fallback disabled when Tree-sitter is active. vim.bo[buf].syntax = "" else -- TreeSitter parser not available, fall back to syntax highlighting diff --git a/lua/codediff/ui/conflict/view/inputs.lua b/lua/codediff/ui/conflict/view/inputs.lua index 852850ab..04a142b0 100644 --- a/lua/codediff/ui/conflict/view/inputs.lua +++ b/lua/codediff/ui/conflict/view/inputs.lua @@ -2,7 +2,6 @@ local M = {} local core = require("codediff.ui.core") -local semantic = require("codediff.ui.semantic_tokens") local config = require("codediff.config") local diff_module = require("codediff.core.diff") @@ -43,10 +42,6 @@ function M.compute_and_render_conflict(original_buf, modified_buf, base_lines, o -- Render merge view with alignment and filler lines local render_result = core.render_merge_view(original_buf, modified_buf, base_to_original_diff, base_to_modified_diff, base_lines, original_lines, modified_lines) - -- Apply semantic tokens (both are virtual buffers in conflict mode) - semantic.apply_semantic_tokens(original_buf, modified_buf) - semantic.apply_semantic_tokens(modified_buf, original_buf) - -- Setup window options with structural scroll-sync (filler lines enable proper alignment) if original_win and modified_win and vim.api.nvim_win_is_valid(original_win) and vim.api.nvim_win_is_valid(modified_win) then vim.wo[original_win].wrap = false diff --git a/lua/codediff/ui/lifecycle/cleanup.lua b/lua/codediff/ui/lifecycle/cleanup.lua index b67ad9e4..c15940d0 100644 --- a/lua/codediff/ui/lifecycle/cleanup.lua +++ b/lua/codediff/ui/lifecycle/cleanup.lua @@ -6,7 +6,6 @@ local keymaps = require("codediff.ui.lifecycle.keymaps") local session = require("codediff.ui.lifecycle.session") local state = require("codediff.ui.lifecycle.state") local welcome_window = require("codediff.ui.view.welcome_window") -local compat = require("codediff.core.compat") -- Autocmd group for cleanup local augroup = vim.api.nvim_create_augroup("codediff_lifecycle", { clear = true }) @@ -57,30 +56,6 @@ local function cleanup_diff(tabpage) pcall(panel_view._cleanup_auto_refresh) end - -- Send didClose notifications for virtual buffers - -- Compute URIs on-demand since we don't store them anymore - local original_virtual_uri = session.compute_virtual_uri(diff.git_root, diff.original_revision, diff.original.relative) - local modified_virtual_uri = session.compute_virtual_uri(diff.git_root, diff.modified_revision, diff.modified.relative) - - -- Get LSP clients from any valid buffer - local ref_bufnr = vim.api.nvim_buf_is_valid(diff.original_bufnr) and diff.original_bufnr or diff.modified_bufnr - local clients = vim.lsp.get_clients({ bufnr = ref_bufnr }) - - for _, client in ipairs(clients) do - if client.server_capabilities.semanticTokensProvider then - if original_virtual_uri then - pcall(compat.lsp_notify, client, "textDocument/didClose", { - textDocument = { uri = original_virtual_uri }, - }) - end - if modified_virtual_uri then - pcall(compat.lsp_notify, client, "textDocument/didClose", { - textDocument = { uri = modified_virtual_uri }, - }) - end - end - end - -- Delete virtual buffers if they're still valid if vim.api.nvim_buf_is_valid(diff.original_bufnr) then if is_virtual_revision(diff.original_revision) then diff --git a/lua/codediff/ui/semantic_tokens.lua b/lua/codediff/ui/semantic_tokens.lua deleted file mode 100644 index 7741fecd..00000000 --- a/lua/codediff/ui/semantic_tokens.lua +++ /dev/null @@ -1,261 +0,0 @@ --- Semantic tokens for diff buffers --- --- IMPORTANT: This module vendors critical functions from Neovim's LSP semantic tokens --- implementation because there is no public API to process semantic token responses --- for arbitrary buffers. --- --- Source: /usr/share/nvim/runtime/lua/vim/lsp/semantic_tokens.lua --- Requires: Neovim 0.9+ (when vim.lsp.semantic_tokens was introduced) --- --- Functions vendored: --- - modifiers_from_number(): Decodes token modifiers from bit field --- - tokens_to_ranges(): Converts LSP token array to highlight ranges --- --- These are copied from Neovim core because: --- 1. No public API exists to process semantic token responses --- 2. STHighlighter class is private and buffer-bound --- --- Version compatibility: --- - Neovim 0.9+: Fully supported --- - Neovim 0.8 and earlier: Gracefully skipped (TreeSitter only) --- --- Future-proofing: --- - The LSP semantic tokens format is stable (LSP spec) --- - Token encoding (delta format) is unlikely to change --- - If Neovim adds public API, we can switch to it --- --- Tested with: Neovim 0.9-0.12 --- -local M = {} - -local api = vim.api -local bit = require("bit") -local compat = require("codediff.core.compat") - --- Namespace for semantic token highlights -local ns_semantic = api.nvim_create_namespace("codediff_semantic_tokens") - --- ============================================================================ --- VENDORED FROM: vim/lsp/semantic_tokens.lua --- ============================================================================ - ---- Extract modifier strings from encoded number ---- @param x integer ---- @param modifiers_table string[] ---- @return table -local function modifiers_from_number(x, modifiers_table) - local modifiers = {} - local idx = 1 - while x > 0 do - if bit.band(x, 1) == 1 then - modifiers[modifiers_table[idx]] = true - end - x = bit.rshift(x, 1) - idx = idx + 1 - end - return modifiers -end - ---- Convert raw token data to highlight ranges ---- @param data integer[] ---- @param bufnr integer ---- @param legend table ---- @param encoding string ---- @return table[] -local function tokens_to_ranges(data, bufnr, legend, encoding) - local token_types = legend.tokenTypes - local token_modifiers = legend.tokenModifiers - local lines = api.nvim_buf_get_lines(bufnr, 0, -1, false) - local eol_offset = vim.bo[bufnr].fileformat == "dos" and 2 or 1 - local ranges = {} - - local line = nil - local start_char = 0 - - -- Data format: [deltaLine, deltaStart, length, tokenType, tokenModifiers] - for i = 1, #data, 5 do - local delta_line = data[i] - line = line and line + delta_line or delta_line - local delta_start = data[i + 1] - start_char = delta_line == 0 and start_char + delta_start or delta_start - - local token_type = token_types[data[i + 3] + 1] - - if token_type then - local modifiers = modifiers_from_number(data[i + 4], token_modifiers) - local end_char = start_char + data[i + 2] - local buf_line = lines[line + 1] or "" - local end_line = line - local start_col = vim.str_byteindex(buf_line, encoding, start_char, false) - - local new_end_char = end_char - vim.str_utfindex(buf_line, encoding) - eol_offset - - -- Handle tokens spanning multiple lines - while new_end_char > 0 do - end_char = new_end_char - end_line = end_line + 1 - buf_line = lines[end_line + 1] or "" - new_end_char = new_end_char - vim.str_utfindex(buf_line, encoding) - eol_offset - end - - local end_col = vim.str_byteindex(buf_line, encoding, end_char, false) - - ranges[#ranges + 1] = { - line = line, - end_line = end_line, - start_col = start_col, - end_col = end_col, - type = token_type, - modifiers = modifiers, - } - end - end - - return ranges -end - --- ============================================================================ --- END VENDORED CODE --- ============================================================================ - ---- Apply highlights to buffer from token ranges ---- @param bufnr integer ---- @param ranges table[] -local function apply_highlights(bufnr, ranges) - -- Clear existing semantic token highlights - api.nvim_buf_clear_namespace(bufnr, ns_semantic, 0, -1) - - for _, token in ipairs(ranges) do - -- Build highlight group name - local hl_group = "@lsp.type." .. token.type - - -- Add modifiers - for modifier, _ in pairs(token.modifiers) do - hl_group = hl_group .. "." .. modifier - end - - -- Apply extmark with semantic token highlight - pcall(api.nvim_buf_set_extmark, bufnr, ns_semantic, token.line, token.start_col, { - hl_group = hl_group, - end_line = token.end_line, - end_col = token.end_col, - priority = vim.hl.priorities and vim.hl.priorities.semantic_tokens or 125, - strict = false, - }) - end -end - ---- Request and apply semantic tokens to left buffer from right buffer's LSP ---- The left buffer is a virtual file (codediff://) which we manually register with LSP ---- @param left_buf integer Left buffer (virtual file with codediff:// URI) ---- @param right_buf integer Right buffer (real file, has LSP attached) ---- @return boolean success -function M.apply_semantic_tokens(left_buf, right_buf) - -- Check Neovim version - semantic tokens added in 0.9 - if not vim.lsp.semantic_tokens then - return false - end - - -- Check for required APIs - if not vim.str_byteindex then - return false - end - - -- Verify both buffers are valid - if not api.nvim_buf_is_valid(left_buf) or not api.nvim_buf_is_valid(right_buf) then - return false - end - - -- Get LSP clients from right buffer - local clients = vim.lsp.get_clients({ bufnr = right_buf }) - if #clients == 0 then - return false - end - - -- Find a client that supports semantic tokens - local client = nil - for _, c in ipairs(clients) do - if c.server_capabilities.semanticTokensProvider then - client = c - break - end - end - - if not client then - return false - end - - -- Get URI and content from left buffer - local left_uri = vim.uri_from_bufnr(left_buf) - local left_lines = api.nvim_buf_get_lines(left_buf, 0, -1, false) - local left_text = table.concat(left_lines, "\n") - - -- Get language ID from right buffer's filetype - local language_id = vim.bo[right_buf].filetype or "text" - - -- First, notify LSP about this virtual file via textDocument/didOpen - local didopen_params = { - textDocument = { - uri = left_uri, - languageId = language_id, - version = 1, - text = left_text, - }, - } - - compat.lsp_notify(client, "textDocument/didOpen", didopen_params) - - -- Now request semantic tokens for this file - local params = { - textDocument = { - uri = left_uri, - }, - } - - -- Make async request for semantic tokens - compat.lsp_request(client, "textDocument/semanticTokens/full", params, function(err, result) - if err then - return - end - - if not result then - return - end - - -- Process response - vim.schedule(function() - -- Verify buffer is still valid - if not api.nvim_buf_is_valid(left_buf) then - return - end - - -- Extract token data - local data = result.data - if not data or #data == 0 then - return - end - - -- Get legend from client capabilities - local legend = client.server_capabilities.semanticTokensProvider.legend - local encoding = client.offset_encoding or "utf-16" - - -- Convert tokens to ranges - local ranges = tokens_to_ranges(data, left_buf, legend, encoding) - - -- Apply highlights - apply_highlights(left_buf, ranges) - end) - end, left_buf) - - return true -end - ---- Clear semantic token highlights from buffer ----@param bufnr integer -function M.clear(bufnr) - if api.nvim_buf_is_valid(bufnr) then - api.nvim_buf_clear_namespace(bufnr, ns_semantic, 0, -1) - end -end - -return M diff --git a/lua/codediff/ui/view/inline_view/render.lua b/lua/codediff/ui/view/inline_view/render.lua index 35d41339..e3a99625 100644 --- a/lua/codediff/ui/view/inline_view/render.lua +++ b/lua/codediff/ui/view/inline_view/render.lua @@ -5,7 +5,6 @@ local lifecycle = require("codediff.ui.lifecycle") local config = require("codediff.config") local diff_module = require("codediff.core.diff") local inline = require("codediff.ui.inline") -local semantic = require("codediff.ui.semantic_tokens") local cursor_util = require("codediff.ui.view.cursor") function M.compute_and_render_inline(modified_buf, original_buf, original_lines, modified_lines, original_is_virtual, modified_is_virtual, modified_win, auto_scroll_to_first_hunk) @@ -23,13 +22,6 @@ function M.compute_and_render_inline(modified_buf, original_buf, original_lines, inline.render_inline_diff(modified_buf, lines_diff, original_lines, modified_lines) - if original_is_virtual then - semantic.apply_semantic_tokens(original_buf, modified_buf) - end - if modified_is_virtual then - semantic.apply_semantic_tokens(modified_buf, original_buf) - end - if modified_win and vim.api.nvim_win_is_valid(modified_win) then vim.wo[modified_win].wrap = false if auto_scroll_to_first_hunk and lines_diff.changes and #lines_diff.changes > 0 then diff --git a/lua/codediff/ui/view/render.lua b/lua/codediff/ui/view/render.lua index c63768f6..45a18136 100644 --- a/lua/codediff/ui/view/render.lua +++ b/lua/codediff/ui/view/render.lua @@ -2,7 +2,6 @@ local M = {} local core = require("codediff.ui.core") -local semantic = require("codediff.ui.semantic_tokens") local config = require("codediff.config") local diff_module = require("codediff.core.diff") local cursor_util = require("codediff.ui.view.cursor") @@ -66,14 +65,6 @@ function M.compute_and_render( -- Render diff highlights core.render_diff(original_buf, modified_buf, original_lines, modified_lines, lines_diff) - -- Apply semantic tokens for virtual buffers - if original_is_virtual then - semantic.apply_semantic_tokens(original_buf, modified_buf) - end - if modified_is_virtual then - semantic.apply_semantic_tokens(modified_buf, original_buf) - end - -- Setup scroll synchronization (only if windows provided) if original_win and modified_win and vim.api.nvim_win_is_valid(original_win) and vim.api.nvim_win_is_valid(modified_win) then -- Save cursor position if we need to preserve it (on update) diff --git a/tests/README.md b/tests/README.md index 7b39f618..cbe9ceb2 100644 --- a/tests/README.md +++ b/tests/README.md @@ -44,14 +44,6 @@ Diff view scrolling behavior: **5 tests** -### ✅ Semantic Tokens (render/semantic_tokens_spec.lua) -LSP integration and rendering: -- Module compatibility checks -- Virtual file URL handling -- Namespace management - -**12 tests** - ## Running Tests ### All tests: diff --git a/tests/ui/semantic_tokens_spec.lua b/tests/ui/semantic_tokens_spec.lua deleted file mode 100644 index c4b571b4..00000000 --- a/tests/ui/semantic_tokens_spec.lua +++ /dev/null @@ -1,229 +0,0 @@ --- Test: Semantic Tokens Rendering --- Validates that our vendored semantic token implementation matches Neovim's behavior - -describe("Semantic Tokens Rendering", function() - -- Test 1: Module loads without errors - it("Module loads successfully", function() - local semantic = require("codediff.ui.semantic_tokens") - assert.is_not_nil(semantic, "Module should load") - assert.equal("function", type(semantic.apply_semantic_tokens), "Should export apply_semantic_tokens") - assert.equal("function", type(semantic.clear), "Should export clear") - end) - - -- Test 2: Version compatibility check - it("Version compatibility check works", function() - local semantic = require("codediff.ui.semantic_tokens") - - -- Should gracefully return false if no clients - local result = semantic.apply_semantic_tokens(1, 1) - assert.equal(false, result, "Should return false when no LSP clients") - - -- On Neovim 0.9+, semantic tokens module should exist - if vim.fn.has('nvim-0.9') == 1 then - assert.is_not_nil(vim.lsp.semantic_tokens, "Neovim 0.9+ should have semantic_tokens") - assert.is_not_nil(vim.str_byteindex, "Neovim 0.9+ should have str_byteindex") - end - end) - - -- Test 3: Vendored modifiers_from_number matches Neovim's implementation - it("modifiers_from_number implementation matches Neovim", function() - -- We need to test our vendored function matches Neovim's behavior - -- Unfortunately the function is local, so we test it indirectly through the full flow - - -- Just verify the bit module is available (required for the function) - local bit = require('bit') - assert.is_not_nil(bit.band, "bit.band should be available") - assert.is_not_nil(bit.rshift, "bit.rshift should be available") - end) - - -- Test 4: Clear function works - it("Clear function works without errors", function() - local semantic = require("codediff.ui.semantic_tokens") - - -- Create a test buffer - local buf = vim.api.nvim_create_buf(false, true) - - -- Should not error when clearing empty buffer - semantic.clear(buf) - - -- Should not error when clearing invalid buffer - vim.api.nvim_buf_delete(buf, { force = true }) - semantic.clear(buf) -- Should handle invalid buffer gracefully - end) - - -- Test 5: Namespace is created correctly - it("Semantic token namespace exists", function() - local semantic = require("codediff.ui.semantic_tokens") - - local namespaces = vim.api.nvim_get_namespaces() - assert.is_not_nil(namespaces.codediff_semantic_tokens, - "codediff_semantic_tokens namespace should be created") - end) - - -- Test 6: Integration test with real LSP (if available) - it("Integration with LSP client (if available)", function() - -- Skip if no LSP support - if not vim.lsp.semantic_tokens then - print(" (skipped: Neovim < 0.9)") - return - end - - local semantic = require("codediff.ui.semantic_tokens") - - -- Create test buffers - local left_buf = vim.api.nvim_create_buf(false, true) - local right_buf = vim.api.nvim_create_buf(false, true) - - -- Set some content - vim.api.nvim_buf_set_lines(left_buf, 0, -1, false, { - "local function test()", - " return 42", - "end" - }) - - vim.api.nvim_buf_set_lines(right_buf, 0, -1, false, { - "local function test()", - " return 42", - "end" - }) - - -- Set filetype to trigger potential LSP - vim.bo[left_buf].filetype = "lua" - vim.bo[right_buf].filetype = "lua" - - -- Try to apply semantic tokens (will fail gracefully if no LSP client) - local result = semantic.apply_semantic_tokens(left_buf, right_buf) - - -- Result should be false if no LSP client attached, which is expected - assert.equal("boolean", type(result), "Should return boolean") - - -- Cleanup - vim.api.nvim_buf_delete(left_buf, { force = true }) - vim.api.nvim_buf_delete(right_buf, { force = true }) - end) - - -- Test 7: Token encoding/decoding consistency - it("Token data structure handling", function() - -- Test that our implementation handles the expected LSP token format - -- LSP tokens are arrays of [deltaLine, deltaStart, length, tokenType, tokenModifiers] - - -- Mock token data (from LSP spec example) - local mock_tokens = { - 0, 5, 4, 1, 0, -- Line 0, col 5, length 4, type 1, no modifiers - 1, 0, 3, 2, 1, -- Line 1, col 0, length 3, type 2, modifier bit 1 - } - - -- Verify array length (should be multiple of 5) - assert.equal(0, #mock_tokens % 5, "Token data should be multiple of 5") - - -- Each token has exactly 5 elements - local token_count = #mock_tokens / 5 - assert.equal(2, token_count, "Should have 2 tokens in mock data") - end) - - -- Test 8: Highlight priority respects Neovim defaults - it("Respects Neovim's semantic token priority", function() - -- Should use vim.hl.priorities.semantic_tokens if available - if vim.hl.priorities and vim.hl.priorities.semantic_tokens then - local priority = vim.hl.priorities.semantic_tokens - assert.equal("number", type(priority), "Priority should be a number") - assert.is_true(priority > 0, "Priority should be positive") - else - -- Fallback for older Neovim versions - local fallback = 125 - assert.equal("number", type(fallback), "Fallback priority should be a number") - end - end) - - -- Test 9: URI handling for scratch buffers - it("URI construction for diff buffers", function() - -- Create a real file buffer - local tmpfile = vim.fn.tempname() .. ".lua" - vim.fn.writefile({"local x = 1"}, tmpfile) - - local buf = vim.fn.bufadd(tmpfile) - vim.fn.bufload(buf) - - -- Get URI - local uri = vim.uri_from_bufnr(buf) - assert.equal("string", type(uri), "URI should be a string") - assert.is_true(uri:match("^file://") ~= nil, "URI should start with file://") - - -- Cleanup - vim.fn.delete(tmpfile) - vim.api.nvim_buf_delete(buf, { force = true }) - end) - - -- Test 10: Graceful handling of missing capabilities - it("Handles missing semantic token capabilities", function() - local semantic = require("codediff.ui.semantic_tokens") - - -- Create buffers with no LSP client - local left_buf = vim.api.nvim_create_buf(false, true) - local right_buf = vim.api.nvim_create_buf(false, true) - - -- Should return false gracefully - local result = semantic.apply_semantic_tokens(left_buf, right_buf) - assert.equal(false, result, "Should return false with no LSP client") - - -- Cleanup - vim.api.nvim_buf_delete(left_buf, { force = true }) - vim.api.nvim_buf_delete(right_buf, { force = true }) - end) - - -- Test 11: Virtual file URL creation and parsing - it("Virtual file URL creation and parsing", function() - local virtual_file = require('codediff.core.virtual_file') - - -- Test URL creation with actual hex commit hash (use platform-agnostic path) - local git_root = vim.fn.has("win32") == 1 and "D:/project" or "/home/user/project" - local commit = "abc123def456" -- Use hex commit hash instead of "HEAD" - local filepath = "src/file.lua" - - local url = virtual_file.create_url(git_root, commit, filepath) - assert.equal("string", type(url), "URL should be a string") - assert.is_true(url:match("^codediff://") ~= nil, "URL should start with codediff://") - - -- Test URL parsing (normalize both for comparison) - local parsed_root, parsed_commit, parsed_path = virtual_file.parse_url(url) - local normalized_parsed = vim.fn.fnamemodify(parsed_root, ':p'):gsub('[/\\]$', ''):gsub('\\', '/') - local normalized_expected = vim.fn.fnamemodify(git_root, ':p'):gsub('[/\\]$', ''):gsub('\\', '/') - assert.equal(normalized_expected, normalized_parsed, "Parsed git root should match") - assert.equal(commit, parsed_commit, "Parsed commit should match") - assert.equal(filepath, parsed_path, "Parsed filepath should match") - - -- Test with different inputs (use platform-agnostic path) - local test_root = vim.fn.has("win32") == 1 and "C:/test" or "/tmp/test" - local url2 = virtual_file.create_url(test_root, "abc123", "test.lua") - local root2, commit2, path2 = virtual_file.parse_url(url2) - -- Normalize both paths for comparison on Windows - local normalized_root2 = vim.fn.fnamemodify(root2, ':p'):gsub('[/\\]$', ''):gsub('\\', '/') - local normalized_test_root = vim.fn.fnamemodify(test_root, ':p'):gsub('[/\\]$', ''):gsub('\\', '/') - assert.equal(normalized_test_root, normalized_root2, "Root should match") - assert.equal("abc123", commit2, "Commit should match") - assert.equal("test.lua", path2, "Path should match") - end) - - -- Test 12: Diagnostics disabled on virtual buffers - it("Diagnostics are disabled on virtual buffers", function() - -- This tests that vim.diagnostic.enable(false) is called on virtual buffers - -- We can't easily test the actual BufReadCmd callback without a real git repo - -- so we test that the API works correctly - - local test_buf = vim.api.nvim_create_buf(false, true) - - -- Initially diagnostics should be enabled - assert.equal(true, vim.diagnostic.is_enabled({bufnr = test_buf}), - "Diagnostics should be enabled by default") - - -- Disable diagnostics - vim.diagnostic.enable(false, {bufnr = test_buf}) - - -- Verify disabled - assert.equal(false, vim.diagnostic.is_enabled({bufnr = test_buf}), - "Diagnostics should be disabled after vim.diagnostic.enable(false)") - - -- Cleanup - vim.api.nvim_buf_delete(test_buf, { force = true }) - end) -end)