-
Notifications
You must be signed in to change notification settings - Fork 79
Suppress compose code lenses for documents outside the workspace #544
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
Draft
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/fix-code-lens-error
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a01bf38
Initial plan
Copilot fa96df2
Initial plan: gate compose code lenses on workspace folder membership
Copilot 6d4aca8
Suppress compose code lenses for documents outside the workspace
Copilot af77fe0
Reorder ServiceStartupCodeLensProvider early exits
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file modified
0
packages/compose-language-service/bin/docker-compose-langserver
100644 → 100755
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/compose-language-service/src/service/utils/isDocumentInWorkspace.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /*!-------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See LICENSE in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import type { WorkspaceFolder } from 'vscode-languageserver'; | ||
| import type { DocumentUri } from 'vscode-languageserver-textdocument'; | ||
| import type { ActionContext } from './ActionContext'; | ||
|
|
||
| /** | ||
| * Determines whether a document is located within one of the workspace folders open in the client. | ||
| * If the client does not support the `workspace/workspaceFolders` request, the document is | ||
| * optimistically treated as being within the workspace (so behavior is unchanged for such clients). | ||
| * @param ctx The current action context (used to access client capabilities and the connection) | ||
| * @param documentUri The URI of the document | ||
| * @returns True if the document is within a workspace folder (or the capability is unsupported), false otherwise | ||
| */ | ||
| export async function isDocumentInWorkspace(ctx: ActionContext, documentUri: DocumentUri): Promise<boolean> { | ||
| // If the client doesn't support workspace folders, we can't verify, so optimistically show code lenses | ||
| if (!ctx.clientCapabilities?.workspace?.workspaceFolders) { | ||
| return true; | ||
| } | ||
|
|
||
| const folders = await ctx.connection.workspace.getWorkspaceFolders(); | ||
| return isDocumentInWorkspaceFolders(documentUri, folders); | ||
| } | ||
|
|
||
| /** | ||
| * Determines whether a document is located within one of the given workspace folders. | ||
| * @param documentUri The URI of the document | ||
| * @param folders The workspace folders reported by the client (may be `null`/`undefined` if none are open) | ||
| * @returns True if the document is within one of the workspace folders, false otherwise | ||
| */ | ||
| export function isDocumentInWorkspaceFolders(documentUri: DocumentUri, folders: WorkspaceFolder[] | null | undefined): boolean { | ||
| if (!folders?.length) { | ||
| return false; | ||
| } | ||
|
|
||
| return folders.some(folder => uriIsWithinFolder(documentUri, folder.uri)); | ||
| } | ||
|
|
||
| function uriIsWithinFolder(documentUri: string, folderUri: string): boolean { | ||
| // Ensure the folder URI ends with a slash so that a folder like `file:///foo` does not match `file:///foobar` | ||
| const normalizedFolderUri = folderUri.endsWith('/') ? folderUri : `${folderUri}/`; | ||
| return documentUri === folderUri || documentUri.startsWith(normalizedFolderUri); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/compose-language-service/src/test/utils/isDocumentInWorkspace.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /*!-------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See LICENSE in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import type { WorkspaceFolder } from 'vscode-languageserver'; | ||
| import { isDocumentInWorkspaceFolders } from '../../service/utils/isDocumentInWorkspace'; | ||
|
|
||
| function folder(uri: string): WorkspaceFolder { | ||
| return { uri, name: uri }; | ||
| } | ||
|
|
||
| describe('(Unit) isDocumentInWorkspaceFolders', () => { | ||
| describe('Common scenarios', () => { | ||
| it('Should return true when the document is directly within a workspace folder', () => { | ||
| isDocumentInWorkspaceFolders('file:///workspace/compose.yaml', [folder('file:///workspace')]).should.be.true; | ||
| }); | ||
|
|
||
| it('Should return true when the document is nested within a workspace folder', () => { | ||
| isDocumentInWorkspaceFolders('file:///workspace/sub/dir/compose.yaml', [folder('file:///workspace')]).should.be.true; | ||
| }); | ||
|
|
||
| it('Should return true when the folder URI has a trailing slash', () => { | ||
| isDocumentInWorkspaceFolders('file:///workspace/compose.yaml', [folder('file:///workspace/')]).should.be.true; | ||
| }); | ||
|
|
||
| it('Should return true when the document is within one of several workspace folders', () => { | ||
| isDocumentInWorkspaceFolders('file:///second/compose.yaml', [folder('file:///first'), folder('file:///second')]).should.be.true; | ||
| }); | ||
| }); | ||
|
|
||
| describe('Negative scenarios', () => { | ||
| it('Should return false when the document is outside all workspace folders', () => { | ||
| isDocumentInWorkspaceFolders('file:///elsewhere/compose.yaml', [folder('file:///workspace')]).should.be.false; | ||
| }); | ||
|
|
||
| it('Should return false for a sibling folder with a matching prefix', () => { | ||
| // `file:///workspace` should not match `file:///workspace-other` | ||
| isDocumentInWorkspaceFolders('file:///workspace-other/compose.yaml', [folder('file:///workspace')]).should.be.false; | ||
| }); | ||
|
|
||
| it('Should return false when there are no workspace folders', () => { | ||
| isDocumentInWorkspaceFolders('file:///workspace/compose.yaml', []).should.be.false; | ||
| }); | ||
|
|
||
| it('Should return false when the folders are null', () => { | ||
| isDocumentInWorkspaceFolders('file:///workspace/compose.yaml', null).should.be.false; | ||
| }); | ||
|
|
||
| it('Should return false when the folders are undefined', () => { | ||
| isDocumentInWorkspaceFolders('file:///workspace/compose.yaml', undefined).should.be.false; | ||
| }); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in af77fe0 by reordering the early exits to check for missing
servicesfirst and cancellation before awaiting the workspace-folder check.