-
Notifications
You must be signed in to change notification settings - Fork 59
feat(code-editor): improve definition navigation #3468
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
qingqing-ux
wants to merge
1
commit into
goplus:ui
Choose a base branch
from
qingqing-ux:issue-3455
base: ui
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import type { IDocumentBase } from './document-base' | ||
| import type { ILSPClient } from './lsp/types' | ||
| import type { ITextDocument } from './common' | ||
| import { HoverProvider } from './hover' | ||
| import { builtInCommandGoToDefinition, builtInCommandViewDefinition } from './ui/code-editor-ui' | ||
|
|
||
| function makeLSPClient(targetUri: string) { | ||
| return { | ||
| textDocumentHover: vi.fn().mockResolvedValue({ | ||
| contents: { kind: 'markdown', value: 'Variable documentation' }, | ||
| range: { | ||
| start: { line: 4, character: 0 }, | ||
| end: { line: 4, character: 8 } | ||
| } | ||
| }), | ||
| getDefinition: vi.fn().mockResolvedValue(null), | ||
| textDocumentDefinition: vi.fn().mockResolvedValue({ | ||
| uri: targetUri, | ||
| range: { | ||
| start: { line: 4, character: 0 }, | ||
| end: { line: 4, character: 8 } | ||
| } | ||
| }), | ||
| textDocumentTypeDefinition: vi.fn().mockResolvedValue(null), | ||
| textDocumentPrepareRename: vi.fn().mockResolvedValue(null) | ||
| } as unknown as ILSPClient | ||
| } | ||
|
|
||
| const documentBase = { | ||
| getDocumentation: vi.fn().mockResolvedValue(null) | ||
| } as unknown as IDocumentBase | ||
|
|
||
| function makeTextDocument(uri: string) { | ||
| return { id: { uri } } as unknown as ITextDocument | ||
| } | ||
|
|
||
| describe('HoverProvider definition actions', () => { | ||
| it('offers view and go-to actions for a definition in another document', async () => { | ||
| const sourceUri = 'file:///source.spx' | ||
| const targetUri = 'file:///target.spx' | ||
| const provider = new HoverProvider(makeLSPClient(targetUri), documentBase) | ||
|
|
||
| const hover = await provider.provideHover( | ||
| { textDocument: makeTextDocument(sourceUri), signal: new AbortController().signal }, | ||
| { line: 5, column: 2 } | ||
| ) | ||
|
|
||
| expect(hover?.actions.map((action) => action.command)).toEqual([ | ||
| builtInCommandViewDefinition, | ||
| builtInCommandGoToDefinition | ||
| ]) | ||
| expect(hover?.actions[0].arguments).toEqual([ | ||
| { | ||
| textDocument: { uri: targetUri }, | ||
| range: { | ||
| start: { line: 5, column: 1 }, | ||
| end: { line: 5, column: 9 } | ||
| } | ||
| } | ||
| ]) | ||
| expect(hover?.actions[1].arguments).toEqual([ | ||
| { | ||
| textDocument: { uri: targetUri }, | ||
| range: { | ||
| start: { line: 5, column: 1 }, | ||
| end: { line: 5, column: 9 } | ||
| } | ||
| }, | ||
| { | ||
| textDocument: { uri: sourceUri }, | ||
| position: { line: 5, column: 2 } | ||
| } | ||
| ]) | ||
| }) | ||
|
|
||
| it('does not offer definition actions for the current location', async () => { | ||
| const sourceUri = 'file:///source.spx' | ||
| const provider = new HoverProvider(makeLSPClient(sourceUri), documentBase) | ||
|
|
||
| const hover = await provider.provideHover( | ||
| { textDocument: makeTextDocument(sourceUri), signal: new AbortController().signal }, | ||
| { line: 5, column: 2 } | ||
| ) | ||
|
|
||
| expect(hover?.actions).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
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
55 changes: 55 additions & 0 deletions
55
spx-gui/src/components/xgo-code-editor/ui/DefinitionPeek.vue
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,55 @@ | ||
| <script setup lang="ts"> | ||
| import { UIIcon } from '@/components/ui' | ||
| import type { TextDocument } from '../text-document' | ||
| import type { Range } from '../common' | ||
| import type { Monaco, MonacoEditor, monaco as tmonaco } from '../monaco' | ||
| import MonacoEditorComp from './MonacoEditor.vue' | ||
| import { toMonacoRange } from './common' | ||
|
|
||
| const props = defineProps<{ | ||
| monaco: Monaco | ||
| options: tmonaco.editor.IStandaloneEditorConstructionOptions | ||
| textDocument: TextDocument | ||
| range: Range | ||
| }>() | ||
|
|
||
| const emit = defineEmits<{ | ||
| close: [] | ||
| }>() | ||
|
|
||
| function handleEditorInit(editor: MonacoEditor) { | ||
| editor.setModel(props.textDocument.monacoTextModel) | ||
| const range = toMonacoRange(props.range) | ||
| editor.setSelection(range) | ||
| editor.revealRangeNearTopIfOutsideViewport(range) | ||
| editor.addCommand(props.monaco.KeyCode.Escape, () => emit('close')) | ||
| editor.focus() | ||
| } | ||
| </script> | ||
|
|
||
| <template> | ||
| <section | ||
| v-radar="{ name: 'Definition preview', desc: `Read-only definition in ${textDocument.id.uri}` }" | ||
| class="min-h-0 flex flex-col overflow-hidden rounded-md border border-dividing-line-2 bg-white shadow-sm" | ||
| > | ||
| <header class="h-10 flex items-center justify-between border-b border-dividing-line-2 px-3"> | ||
| <div class="min-w-0 flex items-center gap-2 text-body-medium"> | ||
| <span class="truncate font-medium text-title"> | ||
| {{ $t({ en: 'Definition in', zh: '定义位于' }) }} {{ $t(textDocument.displayName) }} | ||
| </span> | ||
| <span class="flex-none text-grey-700"> | ||
| · {{ $t({ en: `Line ${range.start.line}`, zh: `第 ${range.start.line} 行` }) }} | ||
| </span> | ||
| </div> | ||
| <button | ||
| v-radar="{ name: 'Close definition preview', desc: 'Close the definition preview' }" | ||
| class="h-8 w-8 flex flex-none cursor-pointer items-center justify-center rounded-sm border-0 bg-transparent hover:bg-grey-300" | ||
| :aria-label="$t({ en: 'Close definition preview', zh: '关闭定义预览' })" | ||
| @click="emit('close')" | ||
| > | ||
| <UIIcon class="h-4 w-4" type="close" /> | ||
| </button> | ||
| </header> | ||
| <MonacoEditorComp class="min-h-0 flex-[1_1_0]" :monaco="monaco" :options="options" @init="handleEditorInit" /> | ||
| </section> | ||
| </template> |
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.
[P3] DefinitionPeek :key omits range.end
DefinitionPeekapplies model/selection/reveal only once inhandleEditorInit; it does not watchtextDocument/range, so it only updates when the:keyforces a full remount. The key isuri:range.start.line:range.start.columnand omitsrange.end. Two "view definition" actions with the same start but a different end range would reuse the same editor instance and show a stale selection. Low likelihood, but either include the full range in the key or add awatchon the props so the component is self-consistent regardless of keying. (Note: the full remount also tears down and recreates the Monaco editor instance on every peek-target change, which is one of the heavier editor operations — a stable instance updated viasetModel/setSelection/revealRangewould avoid that cost.)