Skip to content

Conversation

@DPrakashh
Copy link

@DPrakashh DPrakashh commented Dec 10, 2025

This PR implements snippet completions for control-flow keywords (if, for, while, guard, switch, repeat) when they are used in code completion.

The solution involved updating "CodeCompletionSession.swift" to identify keyword completions (.keyword) and overwrite their "insertText" and "textEdit" with the correct LSP snippet strings, setting the insertTextFormat to .snippet.

This ensures that keywords now contain placeholders (e.g., ${1:condition} and $0) required by LSP clients.

Verification
New unit tests were added to "SwiftCompletionSnippetTests.swift" to explicitly verify:

  1. The "insertTextFormat" is correctly set to ".snippet".
  2. The presence of the required snippet placeholders (e.g., $1, $0).

All local tests, including the focused test (swift test --filter SwiftCompletionSnippetTests/testKeywordIfProvidesSnippet), passed successfully.

Checklist (Based on Contributing Guide)
1-Code has been formatted using `swift format -ipr
2-New tests were added to cover the feature.

Fixes #2118

@DPrakashh
Copy link
Author

Hi @ahoppen , @bnbarham , @hamishknight , @rintaro

Hope you're having a good week.

I wanted to check on the status of my PR from four days ago. I know these core completion changes take time to review carefully.
Just confirming that all local tests passed on my end. Would it be possible to get the CI tests triggered when a maintainer is able to look at it?

Thanks for your time!

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Thank you. The overall structure looks good, I have left a few comments inline.

Also, do you have a screen recording or something like that how this looks like in VS Code? Ie. point VS Code to a locally built version of SourceKit-LSP as described in https://github.com/swiftlang/sourcekit-lsp/blob/main/CONTRIBUTING.md#using-a-locally-built-sourcekit-lsp-in-an-editor.

@DPrakashh
Copy link
Author

DPrakashh commented Dec 17, 2025

@ahoppen on it sir

@DPrakashh
Copy link
Author

DPrakashh commented Dec 18, 2025

@ahoppen

I have updated the PR to address all previous feedback. Here is a summary of the changes:

1-Dynamic Indentation: Implemented indentationUnitString() to use inferred indentation (spaces or tabs) instead of hardcoded tab characters.
2-Refactored Completion Logic: Simplified CodeCompletionSession to calculate textEdit once, using either the standard text or the keyword snippet as appropriate.

Test Improvements:
1-Added the required copyright header to SwiftCompletionSnippetTests.swift.
2-Updated test assertions to use try XCTUnwrap for better error handling and readability.
3-Added new test cases to verify snippet behavior with both inferred spaces and inferred tabs.

Verification: The attached video demonstrates:
1-Running swift test --filter SwiftCompletionSnippetTests with all tests passing.
2-The if, while, and for snippets expanding correctly in VS Code.
3-The snippets dynamically adjusting their indentation when switching the editor from Spaces to Tabs.

Thank you for the guidance on this! As this is my first contribution to a project of this scale, I appreciate your patience and would be happy to make further adjustments if needed.

final.1.mp4

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Thanks for your update. The code looks great, we are just in the nitpicking phase now to put the final polish on it.

I think your screen recording still uses the built-in snippets from VS Code instead of the completions from SourceKit-LSP. If you open a file in a SwiftPM project and open the code completion dropdown menu after typing if (Ctrl-Space), you should see one item with an if description on the right and which contains Swift Language Basics in its documentation. This is provided by VS Code itself and the plain if entry is the one from SourceKit-LSP. That’s a good reminder that we should ask the VS Code extension to remove this default snippet. Anyway, my biggest concern was that VS Code would not add the indentation of the current line to the subsequent lines inserted by the completion item but that concern is addressed because I forgot that we already had multi-line completion items (the ones that already use indentationWidth).

Image

@DPrakashh
Copy link
Author

@ahoppen

I have finalised the refinements based on your feedback. I am attaching a video demo (recorded with Screencast Mode) which demonstrates the comparison between the default VS Code snippets and the updated SourceKit-LSP snippets.

Changes implemented based on your suggestions:
1-Logical Tab Order: Reordered the tab stops so the cursor lands on the condition first ($1) and moves to the body last ($0). This ensures the user fills out the logic before jumping into the braces.
2-Switch Statement Alignment: Corrected the switch snippet so that case labels align vertically with the switch keyword, fixing the previous double-indentation issue.
3-Code Cleanup: Removed the unused doubleIndent variable and the legacy indentation helper functions, opting for a cleaner implementation using indentationWidth.
4-Refined Test Suite: Updated CodeCompletionSnippetTests to be more resilient, verifying the presence of specific keywords and placeholders rather than exact indentation strings.

Usability Improvement:
1-Descriptive "Body" Placeholder: I added a small improvement for the user by changing the final stop from an empty ${0:} to a descriptive ${0:body}. This provides a clear visual hint of where the code should go while remaining the final exit point for the snippet.

I have verified these changes using a locally built SourceKit-LSP server in VS Code as shown in the video. Ready for your final review!

Note: I’ve done my best to address all the points from the review. If there are any further refinements or "nits" you'd like me to fix to get this fully aligned with the project's standards, please let me know—I’m happy to make additional changes!
https://github.com/user-attachments/assets/f14026c9-c66b-4c39-b836-8aa5dcbdd270

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Looks great to me. Thank you @DPrakashh ❤️

@ahoppen
Copy link
Member

ahoppen commented Dec 19, 2025

@swift-ci Please test

@ahoppen
Copy link
Member

ahoppen commented Dec 19, 2025

@swift-ci Please test Windows

@DPrakashh
Copy link
Author

Thank you so much for the guidance and the review, @ahoppen I am glad the refinements and the added placeholders look good. I will keep an eye on the CI tests for macOS and Windows. Excited to see this merged!!!

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.

Provide if, for, while, etc. as snippets instead of plain keywords

2 participants