🐛 fix(vscode): 代码阅读注释里的链接指到别的文件时用预览打开,和 ⌘ 点击跳到定义走同一条路
This commit is contained in:
1 parent
e04a90c877
commit
bc7380c716
9 files changed
+123
-72
No files matched your search
@@ -12,8 +12,9 @@ import type { TreeSitterRuntime } from '../runtime'
|
||||
import type { ImportedName, LinkTarget, SymbolNode } from '../symbolLinks'
|
||||
import type { IndexedEntry } from '../workspaceIndex'
|
||||
import type { BlockFilter } from './blockFilter'
|
||||
import type { Definition, FoundLocation } from './definitions'
|
||||
import type { FoundLocation } from './definitions'
|
||||
import type { ExtensionToWebview, Occurrence, Reveal, WebviewToExtension } from './messages'
|
||||
import type { TargetSpot } from './openTarget'
|
||||
import { realpath } from 'node:fs/promises'
|
||||
import { dirname } from 'node:path'
|
||||
import { commands, Range, Uri, window, workspace, WorkspaceEdit } from 'vscode'
|
||||
@@ -37,8 +38,9 @@ import { DefinitionSources } from './definitionSources'
|
||||
import { documentMessage } from './documentMessage'
|
||||
import { NavigationContext, OCCURRENCES_CONTEXT } from './occurrenceNavigation'
|
||||
import { occurrencesOf } from './occurrences'
|
||||
import { languageOf, openingFor } from './openTarget'
|
||||
import { pathPreviewOf } from './pathPreview'
|
||||
import { findRepoSymbol, isImageFile, isRepoImagePath, repoRootOf, resolveRepoEntry } from './repoPath'
|
||||
import { findRepoSymbol, isRepoImagePath, repoRootOf, resolveRepoEntry } from './repoPath'
|
||||
|
||||
export const PREVIEW_VIEW_TYPE = 'superpowers.codeReader.preview'
|
||||
|
||||
@@ -620,8 +622,7 @@ class Preview {
|
||||
if (version !== document.version)
|
||||
return
|
||||
const current = document.uri.toString()
|
||||
const supports = (languageId: string): boolean => this.registry.languageFor(languageId) !== undefined
|
||||
let action = definitionAction(await findDefinitions(await this.definitions.locate(version, offset), supports), current)
|
||||
let action = definitionAction(await findDefinitions(await this.definitions.locate(version, offset)), current)
|
||||
if (action.kind === 'pick') {
|
||||
const picked = await pickDefinition(action.targets)
|
||||
action = definitionAction(picked ? [picked] : [], current)
|
||||
@@ -629,13 +630,12 @@ class Preview {
|
||||
if (action.kind === 'none' || action.kind === 'pick')
|
||||
return
|
||||
const { target } = action
|
||||
const reveal: Reveal = { line: target.line, definition: target.offset }
|
||||
if (action.kind === 'here') {
|
||||
this.reveal(reveal)
|
||||
this.reveal({ line: target.line, definition: target.offset })
|
||||
return
|
||||
}
|
||||
try {
|
||||
await this.openDefinition(action.kind, target, reveal)
|
||||
await this.openElsewhere(target.location.uri, { range: target.location.range, offset: target.offset })
|
||||
}
|
||||
catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
@@ -645,18 +645,24 @@ class Preview {
|
||||
}
|
||||
|
||||
/**
|
||||
* 和普通编辑器里 ⌘ + 点击一样:在同一个编辑器组打开目标(是否按「预览标签」打开听 VS Code 的设置),
|
||||
* 并按 workbench.editor.enablePreviewFromCodeNavigation 把当前这个预览标签固定住,免得被目标替掉。
|
||||
* 在预览所在的编辑器组打开别的文件,和普通编辑器里 ⌘ + 点击一样:语言有代码阅读的用预览打开,给了 spot 就滚过去、闪这处;
|
||||
* 其余用 VS Code 默认的打开方式,给了 spot 就选中它(见 openingFor)。目标是否按「预览标签」打开听 VS Code 的设置,
|
||||
* workbench.editor.enablePreviewFromCodeNavigation 为假时先把当前这个预览标签固定住,免得被目标替掉。
|
||||
*/
|
||||
private async openDefinition(kind: 'preview' | 'text', target: Definition, reveal: Reveal): Promise<void> {
|
||||
private async openElsewhere(uri: Uri, spot?: TargetSpot): Promise<void> {
|
||||
if (this.panel.active && !workspace.getConfiguration('workbench.editor').get<boolean>('enablePreviewFromCodeNavigation', false))
|
||||
await commands.executeCommand('workbench.action.keepEditor')
|
||||
const options: TextDocumentShowOptions = { viewColumn: this.panel.viewColumn }
|
||||
const { uri, range } = target.location
|
||||
if (kind === 'text')
|
||||
await window.showTextDocument(uri, { ...options, selection: range })
|
||||
const opening = openingFor(await languageOf(uri), languageId => this.registry.languageFor(languageId) !== undefined, spot)
|
||||
if (opening.kind === 'default') {
|
||||
await commands.executeCommand('vscode.open', uri, { ...options, ...(opening.selection && { selection: opening.selection }) })
|
||||
return
|
||||
}
|
||||
const open = (): Thenable<unknown> => commands.executeCommand('vscode.openWith', uri, PREVIEW_VIEW_TYPE, options)
|
||||
if (opening.reveal)
|
||||
await this.owner.openAt(uri, opening.reveal, open)
|
||||
else
|
||||
await this.owner.openAt(uri, reveal, () => commands.executeCommand('vscode.openWith', uri, PREVIEW_VIEW_TYPE, options))
|
||||
await open()
|
||||
}
|
||||
|
||||
/** 预览里这批仓库路径,哪些文件或目录不在;这批不带路径的引用,哪些找不到。 */
|
||||
@@ -754,7 +760,7 @@ class Preview {
|
||||
return
|
||||
}
|
||||
if (symbol === undefined) {
|
||||
await this.showRepoFile(uri)
|
||||
await this.openElsewhere(uri)
|
||||
this.markRepoLinkOpened(rel)
|
||||
return
|
||||
}
|
||||
@@ -818,25 +824,14 @@ class Preview {
|
||||
}
|
||||
}
|
||||
|
||||
/** 跳到链接指的符号:在这份文件里就在本预览里滚过去、闪一下;别的文件在预览所在的编辑器组打开、选中符号的名字。 */
|
||||
/** 跳到链接指的符号:在这份文件里就在本预览里滚过去、闪一下;别的文件同 ⌘ + 点击跳到定义(见 openElsewhere)。 */
|
||||
private async showLinkTarget({ uri, selection }: LinkTarget): Promise<void> {
|
||||
if (uri === this.document.uri.toString()) {
|
||||
this.reveal({ line: this.document.positionAt(selection.startIndex).line, definition: selection.startIndex })
|
||||
return
|
||||
}
|
||||
const doc = await workspace.openTextDocument(Uri.parse(uri))
|
||||
await this.showRepoFile(doc.uri, new Range(doc.positionAt(selection.startIndex), doc.positionAt(selection.endIndex)))
|
||||
}
|
||||
|
||||
/** 在预览所在的编辑器组打开文件,并把预览标签固定住,免得被它替掉。图片用默认打开方式,才看得到图。 */
|
||||
private async showRepoFile(uri: Uri, selection?: Range): Promise<void> {
|
||||
if (this.panel.active && !workspace.getConfiguration('workbench.editor').get<boolean>('enablePreviewFromCodeNavigation', false))
|
||||
await commands.executeCommand('workbench.action.keepEditor')
|
||||
if (selection === undefined && isImageFile(uri.fsPath)) {
|
||||
await commands.executeCommand('vscode.open', uri, { viewColumn: this.panel.viewColumn })
|
||||
return
|
||||
}
|
||||
await window.showTextDocument(uri, { viewColumn: this.panel.viewColumn, ...(selection && { selection }) })
|
||||
await this.openElsewhere(doc.uri, { range: new Range(doc.positionAt(selection.startIndex), doc.positionAt(selection.endIndex)), offset: selection.startIndex })
|
||||
}
|
||||
|
||||
private markRepoLinkBroken(path: string | undefined, symbol?: string): void {
|
||||
|
||||
Reference in new issue
Block a user