Repository navigation
feat(catalog): make resolved version display configurable #158
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
Open
hyoban
wants to merge
2
commits into
npmx-dev:main
Choose a base branch
from
hyoban:feat/catalog-display
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import type { CancellationToken, InlayHint } from 'vscode' | ||
| import { createTextDocument } from 'jest-mock-vscode' | ||
| import { afterEach, describe, expect, it, vi } from 'vitest' | ||
| import { Position, Range, Uri, workspace } from 'vscode' | ||
| import { provideCatalogInlayHints } from './catalog-display' | ||
|
|
||
| describe('catalog display', () => { | ||
| const document = createTextDocument(Uri.file('/workspace/package.json'), '{"dependencies":{"vue":"catalog:"}}', 'json') | ||
| const range = new Range(0, 0, 0, document.getText().length) | ||
| const token: CancellationToken = { | ||
| isCancellationRequested: false, | ||
| onCancellationRequested: vi.fn(() => ({ dispose: vi.fn() })), | ||
| } | ||
| const hints: InlayHint[] = [{ position: new Position(0, 32), label: '^3.5.0' }] | ||
|
|
||
| afterEach(async () => { | ||
| await workspace.getConfiguration().update('npmx.catalog.display', undefined) | ||
| }) | ||
|
|
||
| it('keeps hints hidden by default and after switching back to decorations', async () => { | ||
| const next = vi.fn(() => hints) | ||
| expect(await provideCatalogInlayHints(document, range, token, next)).toEqual([]) | ||
| expect(next).not.toHaveBeenCalled() | ||
|
|
||
| await workspace.getConfiguration().update('npmx.catalog.display', 'inlay-hint') | ||
| expect(await provideCatalogInlayHints(document, range, token, next)).toEqual(hints) | ||
| expect(next).toHaveBeenCalledWith(document, range, token) | ||
|
|
||
| next.mockClear() | ||
| await workspace.getConfiguration().update('npmx.catalog.display', 'decoration') | ||
| expect(await provideCatalogInlayHints(document, range, token, next)).toEqual([]) | ||
| expect(next).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('discards hints if the display mode changes while a request is pending', async () => { | ||
| await workspace.getConfiguration().update('npmx.catalog.display', 'inlay-hint') | ||
| const pending = Promise.withResolvers<InlayHint[]>() | ||
| const result = provideCatalogInlayHints(document, range, token, () => pending.promise) | ||
|
|
||
| await workspace.getConfiguration().update('npmx.catalog.display', 'decoration') | ||
| pending.resolve(hints) | ||
|
|
||
| expect(await result).toEqual([]) | ||
| }) | ||
| }) |
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,15 @@ | ||
| import type { Middleware } from '@volar/vscode' | ||
| import { configs } from 'npmx-shared/meta' | ||
| import { workspace } from 'vscode' | ||
|
|
||
| export function usesCatalogInlayHints(): boolean { | ||
| return workspace.getConfiguration().get(configs.catalogDisplay.key, configs.catalogDisplay.default) === 'inlay-hint' | ||
| } | ||
|
|
||
| export const provideCatalogInlayHints: NonNullable<Middleware['provideInlayHints']> = async (document, range, token, next) => { | ||
| if (!usesCatalogInlayHints()) | ||
| return [] | ||
|
|
||
| const hints = await next(document, range, token) | ||
| return usesCatalogInlayHints() ? hints : [] | ||
| } | ||
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 |
|---|---|---|
| @@ -1,52 +1,64 @@ | ||
| import type { BaseLanguageClient } from '@volar/vscode' | ||
| import type { DecorationOptions } from 'vscode' | ||
| import type { DecorationOptions, TextEditor } from 'vscode' | ||
| import { isPackageManifest } from 'npmx-language-core/utils' | ||
| import { useActiveTextEditor, useEditorDecorations, watch } from 'reactive-vscode' | ||
| import { Range } from 'vscode' | ||
| import { configs } from 'npmx-shared/meta' | ||
| import { useActiveTextEditor, useDisposable, useEditorDecorations, useEvent, watch } from 'reactive-vscode' | ||
| import { Range, window, workspace } from 'vscode' | ||
| import { logger } from '#state' | ||
| import { offsetRangeToRange } from '#utils/ast' | ||
| import { getResolvedDependencies } from '#utils/request' | ||
| import { usesCatalogInlayHints } from './catalog-display' | ||
|
|
||
| export function useDecorators(client: BaseLanguageClient) { | ||
| const activeEditor = useActiveTextEditor() | ||
|
|
||
| const { update } = useEditorDecorations( | ||
| activeEditor, | ||
| { | ||
| after: { color: 'rgba(136, 136, 136, 0.63)' }, | ||
| }, | ||
| async (editor) => { | ||
| const document = editor.document | ||
| if (!isPackageManifest(document.uri.path)) | ||
| return [] | ||
| logger.info(`[decorators] updating ${document.uri.path}`) | ||
|
|
||
| const dependencies = await getResolvedDependencies(client, document.uri) | ||
| if (!dependencies) | ||
| return [] | ||
|
|
||
| const result: DecorationOptions[] = [] | ||
|
|
||
| for (const dep of dependencies) { | ||
| if (dep.protocol !== 'catalog') | ||
| continue | ||
|
|
||
| const range = offsetRangeToRange(document, dep.specRange) | ||
| const line = range.end.line | ||
| const len = document.lineAt(line).text.length | ||
| result.push({ | ||
| range: new Range(line, 0, line, len), | ||
| renderOptions: { | ||
| after: { | ||
| contentText: `\t\t ${dep.resolvedSpec}`, | ||
| }, | ||
| const decorationType = useDisposable(window.createTextEditorDecorationType({ | ||
| after: { color: 'rgba(136, 136, 136, 0.63)' }, | ||
| })) | ||
| const { update } = useEditorDecorations(activeEditor, decorationType, getDecorations) | ||
|
|
||
| async function getDecorations(editor: TextEditor): Promise<DecorationOptions[]> { | ||
| if (usesCatalogInlayHints()) | ||
| return [] | ||
|
|
||
| const document = editor.document | ||
| if (!isPackageManifest(document.uri.path)) | ||
| return [] | ||
| logger.info(`[decorators] updating ${document.uri.path}`) | ||
|
|
||
| const dependencies = await getResolvedDependencies(client, document.uri) | ||
| if (!dependencies || usesCatalogInlayHints()) | ||
| return [] | ||
|
|
||
| const result: DecorationOptions[] = [] | ||
|
|
||
| for (const dep of dependencies) { | ||
| if (dep.protocol !== 'catalog') | ||
| continue | ||
|
|
||
| const range = offsetRangeToRange(document, dep.specRange) | ||
| const line = range.end.line | ||
| const len = document.lineAt(line).text.length | ||
| result.push({ | ||
| range: new Range(line, 0, line, len), | ||
| renderOptions: { | ||
| after: { | ||
| contentText: `\t\t ${dep.resolvedSpec}`, | ||
| }, | ||
| }) | ||
| } | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| return result | ||
| }, | ||
| ) | ||
| return result | ||
| } | ||
|
|
||
| watch(activeEditor, update) | ||
| useEvent(workspace.onDidChangeConfiguration, [async (event) => { | ||
| if (!event.affectsConfiguration(configs.catalogDisplay.key)) | ||
| return | ||
|
|
||
| await Promise.all(window.visibleTextEditors.map(async (editor) => { | ||
| editor.setDecorations(decorationType, await getDecorations(editor)) | ||
| })) | ||
| }]) | ||
| } |
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
Oops, something went wrong.
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
git diff dee1eb4b1585699d0c7ff4babd8cdcf5c13aa803 2217edb492a9c924b37a39ce7de54575517aebd1 -- extensions/vscode/src/providers/catalog-display.ts extensions/vscode/src/client.ts extensions/vscode/src/providers/decorators.ts rg -n 'inlayHint|inlayHints|refresh|didChangeConfiguration' packages/language-service/src extensions/vscode/srcRepository: npmx-dev/vscode-npmx
Length of output: 5953
🏁 Script executed:
Repository: npmx-dev/vscode-npmx
Length of output: 41635
🏁 Script executed:
Repository: npmx-dev/vscode-npmx
Length of output: 20197
🌐 Web query:
vscode-languageclient 9.0.1 inlay hints middleware onDidChangeInlayHints refresh implementation💡 Result:
🌐 Web query:
site:github.com/microsoft/vscode-languageserver-node v9.0.1 client provideInlayHints onDidChangeInlayHints💡 Result:
🏁 Script executed:
Repository: npmx-dev/vscode-npmx
Length of output: 11714
🏁 Script executed:
Repository: npmx-dev/vscode-npmx
Length of output: 828
🏁 Script executed:
Repository: npmx-dev/vscode-npmx
Length of output: 2444
🏁 Script executed:
Repository: npmx-dev/vscode-npmx
Length of output: 366
🏁 Script executed:
Repository: npmx-dev/vscode-npmx
Length of output: 29514
🏁 Script executed:
Repository: npmx-dev/vscode-npmx
Length of output: 14340
Refresh inlay hints when
npmx.catalog.displaychanges.provideCatalogInlayHintsfilters only future requests. It does not invalidate the result already held by VS Code. When the setting changes, VS Code can keep empty hints after switching to"inlay-hint", or keep old hints after switching to"decoration". This violates the documented no-reload behaviour.Send the standard
InlayHintRefreshRequestwhen configuration changes. The pinned language client handles this request and fires its inlay-hint provider change event.Suggested fix
import type { ClientFeatures } from 'npmx-language-service/types' +import { InlayHintRefreshRequest } from '@volar/language-server' import { createConnection, createServer, createSimpleProject } from '@volar/language-server/node' import { createNpmxLanguageServicePlugins } from 'npmx-language-service' import { DEFAULT_CLIENT_FEATURES } from 'npmx-language-service/types' @@ connection.onInitialized(() => { connection.console.info('npmx language server initialized') server.initialized() }) + connection.onDidChangeConfiguration(() => { + void connection.sendRequest(InlayHintRefreshRequest.type) + }) connection.onShutdown(server.shutdown)