Skip to content

Commit 0442261

Browse files
committed
fix: fix bug number of bugs in track document positions
1 parent 0e79bcf commit 0442261

File tree

1 file changed

+56
-30
lines changed

1 file changed

+56
-30
lines changed

src/features/trackPositions.ts

+56-30
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,51 @@ export const enableIf: keyof Settings = 'trackDocumentPositions.enable'
77
export default () => {
88
const disposables = [] as vscode.Disposable[]
99

10-
type OurKind = vscode.TextEditorSelectionChangeKind | 'focus-out' | 'text-change'
10+
type OurKind = vscode.TextEditorSelectionChangeKind | 'focus-out' | 'text-change' | 'editor-open'
1111

1212
type History = Array<[pos: vscode.Position, kind?: OurKind]>
1313

14-
const historyPerEditor = new Map<vscode.Uri, History>()
14+
const historyPerEditor = new Map<string, History>()
1515
let history: History
16+
let previousUri: string | undefined
17+
let currentUri: string | undefined
1618
let onHistoryUpdate: (() => void) | undefined
19+
1720
const changeHistory = () => {
1821
const editor = vscode.window.activeTextEditor
19-
if (!editor) return
20-
const key = editor.document.uri
22+
if (!editor || editor.document.uri.toString() === currentUri) return
23+
const key = editor.document.uri.toString()
2124
history = historyPerEditor.get(key) ?? []
2225
historyPerEditor.set(key, history)
26+
previousUri = currentUri
27+
currentUri = key.toString()
2328
}
2429

25-
let writePositions = true
30+
let dontWriteTillNextChange = false
31+
let lastPosition: vscode.Position | undefined
2632

2733
const pushPosition = (kind?: OurKind, forceAdd = false) => {
28-
if (!writePositions) return
29-
const editor = vscode.window.activeTextEditor
30-
if (!editor) return
31-
const position = editor.selection.active
32-
const lastPos = history.at(-1)?.[0]
33-
if (!forceAdd && lastPos && position.isEqual(lastPos)) return
34+
if (dontWriteTillNextChange) return
35+
const position = kind === 'focus-out' ? lastPosition : vscode.window.activeTextEditor?.selection.active
36+
const lastHistoryPos = history.at(-1)?.[0]
37+
if (!position) return
38+
if (!forceAdd && lastHistoryPos && position.isEqual(lastHistoryPos)) return
3439
history.push([position, kind])
3540
onHistoryUpdate?.()
3641
}
3742

3843
vscode.window.onDidChangeTextEditorSelection(({ kind, textEditor: editor, selections }) => {
3944
if (editor !== vscode.window.activeTextEditor || !editor.selection.start.isEqual(editor.selection.end)) return
45+
if (editor.document.uri.toString() !== currentUri) return
46+
if (dontWriteTillNextChange) {
47+
dontWriteTillNextChange = false
48+
return
49+
}
50+
4051
// todo
4152
if (selections.length > 1) return
53+
lastPosition = editor.selection.active
54+
4255
const last = history.at(-1)
4356
const pos = editor.selection.active
4457
let minorChange = false
@@ -61,7 +74,7 @@ export default () => {
6174

6275
const keyPosCandidate = isLineStart || isLineEnd || isFileStart || isFileEnd
6376

64-
pushPosition(keyPosCandidate && last?.[1] === undefined ? undefined : kind, true)
77+
pushPosition(keyPosCandidate && last?.[1] === undefined ? undefined : kind)
6578
}, disposables)
6679

6780
vscode.workspace.onDidChangeTextDocument(({ document }) => {
@@ -74,7 +87,7 @@ export default () => {
7487
}, disposables)
7588

7689
changeHistory()
77-
pushPosition()
90+
pushPosition('editor-open')
7891

7992
vscode.window.onDidChangeActiveTextEditor(() => {
8093
pushPosition('focus-out')
@@ -85,38 +98,51 @@ export default () => {
8598
const editor = vscode.window.activeTextEditor
8699
if (!editor) return
87100
const { uri } = editor.document
88-
const history = historyPerEditor.get(uri)
101+
const history = historyPerEditor.get(uri.toString())
89102
if (!history) return
90103
const getItems = () =>
91-
history.map(
92-
([pos, kind], index): VSCodeQuickPickItem<number> => ({
93-
label: `${pos.line + 1}:${pos.character + 1}`,
94-
description: typeof kind === 'string' || kind === undefined ? kind : vscode.TextEditorSelectionChangeKind[kind],
95-
value: index,
96-
}),
97-
)
104+
history
105+
.map(
106+
([pos, kind], index): VSCodeQuickPickItem<vscode.Position> => ({
107+
label: `${pos.line + 1}:${pos.character + 1}`,
108+
description: typeof kind === 'string' || kind === undefined ? kind : vscode.TextEditorSelectionChangeKind[kind],
109+
value: pos,
110+
}),
111+
)
112+
.reverse()
98113

99114
const currentPos = editor.selection.active
100-
const index = await showQuickPick(getItems(), {
115+
const selected = await showQuickPick(getItems(), {
101116
title: 'Tracked Positions Stack',
102117
ignoreFocusOut: true,
118+
buttons: [
119+
{
120+
// close button
121+
iconPath: new vscode.ThemeIcon('close'),
122+
tooltip: 'Close',
123+
},
124+
],
125+
onDidTriggerButton() {
126+
// clear history
127+
history.splice(0, history.length)
128+
onHistoryUpdate?.()
129+
// void vscode.commands.executeCommand('workbench.action.closeQuickOpen')
130+
},
103131
onDidShow() {
104132
onHistoryUpdate = () => {
105133
this.items = getItems()
106134
}
107135
},
108-
onDidChangeFirstActive(item, index) {
109-
writePositions = false
110-
const [selectedPos] = history[index]!
136+
onDidChangeFirstActive(item) {
137+
dontWriteTillNextChange = true
138+
// const [selectedPos] = history[item.value]!
139+
const selectedPos = item.value
111140
editor.selection = new vscode.Selection(selectedPos, selectedPos)
112141
editor.revealRange(new vscode.Range(selectedPos, selectedPos))
113-
setTimeout(() => {
114-
writePositions = true
115-
})
116142
},
117143
})
118144
onHistoryUpdate = undefined
119-
if (index === undefined) {
145+
if (selected === undefined) {
120146
editor.selection = new vscode.Selection(currentPos, currentPos)
121147
editor.revealRange(new vscode.Range(currentPos, currentPos))
122148
return
@@ -129,7 +155,7 @@ export default () => {
129155
const editor = vscode.window.activeTextEditor
130156
if (!editor) return
131157
const { uri } = editor.document
132-
const history = historyPerEditor.get(uri)
158+
const history = historyPerEditor.get(uri.toString())
133159
if (!history) return
134160
const [selectedPos] = history.at(-2) ?? []
135161
history.pop()

0 commit comments

Comments
 (0)