@@ -7,38 +7,51 @@ export const enableIf: keyof Settings = 'trackDocumentPositions.enable'
7
7
export default ( ) => {
8
8
const disposables = [ ] as vscode . Disposable [ ]
9
9
10
- type OurKind = vscode . TextEditorSelectionChangeKind | 'focus-out' | 'text-change'
10
+ type OurKind = vscode . TextEditorSelectionChangeKind | 'focus-out' | 'text-change' | 'editor-open'
11
11
12
12
type History = Array < [ pos : vscode . Position , kind ?: OurKind ] >
13
13
14
- const historyPerEditor = new Map < vscode . Uri , History > ( )
14
+ const historyPerEditor = new Map < string , History > ( )
15
15
let history : History
16
+ let previousUri : string | undefined
17
+ let currentUri : string | undefined
16
18
let onHistoryUpdate : ( ( ) => void ) | undefined
19
+
17
20
const changeHistory = ( ) => {
18
21
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 ( )
21
24
history = historyPerEditor . get ( key ) ?? [ ]
22
25
historyPerEditor . set ( key , history )
26
+ previousUri = currentUri
27
+ currentUri = key . toString ( )
23
28
}
24
29
25
- let writePositions = true
30
+ let dontWriteTillNextChange = false
31
+ let lastPosition : vscode . Position | undefined
26
32
27
33
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
34
39
history . push ( [ position , kind ] )
35
40
onHistoryUpdate ?.( )
36
41
}
37
42
38
43
vscode . window . onDidChangeTextEditorSelection ( ( { kind, textEditor : editor , selections } ) => {
39
44
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
+
40
51
// todo
41
52
if ( selections . length > 1 ) return
53
+ lastPosition = editor . selection . active
54
+
42
55
const last = history . at ( - 1 )
43
56
const pos = editor . selection . active
44
57
let minorChange = false
@@ -61,7 +74,7 @@ export default () => {
61
74
62
75
const keyPosCandidate = isLineStart || isLineEnd || isFileStart || isFileEnd
63
76
64
- pushPosition ( keyPosCandidate && last ?. [ 1 ] === undefined ? undefined : kind , true )
77
+ pushPosition ( keyPosCandidate && last ?. [ 1 ] === undefined ? undefined : kind )
65
78
} , disposables )
66
79
67
80
vscode . workspace . onDidChangeTextDocument ( ( { document } ) => {
@@ -74,7 +87,7 @@ export default () => {
74
87
} , disposables )
75
88
76
89
changeHistory ( )
77
- pushPosition ( )
90
+ pushPosition ( 'editor-open' )
78
91
79
92
vscode . window . onDidChangeActiveTextEditor ( ( ) => {
80
93
pushPosition ( 'focus-out' )
@@ -85,38 +98,51 @@ export default () => {
85
98
const editor = vscode . window . activeTextEditor
86
99
if ( ! editor ) return
87
100
const { uri } = editor . document
88
- const history = historyPerEditor . get ( uri )
101
+ const history = historyPerEditor . get ( uri . toString ( ) )
89
102
if ( ! history ) return
90
103
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 ( )
98
113
99
114
const currentPos = editor . selection . active
100
- const index = await showQuickPick ( getItems ( ) , {
115
+ const selected = await showQuickPick ( getItems ( ) , {
101
116
title : 'Tracked Positions Stack' ,
102
117
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
+ } ,
103
131
onDidShow ( ) {
104
132
onHistoryUpdate = ( ) => {
105
133
this . items = getItems ( )
106
134
}
107
135
} ,
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
111
140
editor . selection = new vscode . Selection ( selectedPos , selectedPos )
112
141
editor . revealRange ( new vscode . Range ( selectedPos , selectedPos ) )
113
- setTimeout ( ( ) => {
114
- writePositions = true
115
- } )
116
142
} ,
117
143
} )
118
144
onHistoryUpdate = undefined
119
- if ( index === undefined ) {
145
+ if ( selected === undefined ) {
120
146
editor . selection = new vscode . Selection ( currentPos , currentPos )
121
147
editor . revealRange ( new vscode . Range ( currentPos , currentPos ) )
122
148
return
@@ -129,7 +155,7 @@ export default () => {
129
155
const editor = vscode . window . activeTextEditor
130
156
if ( ! editor ) return
131
157
const { uri } = editor . document
132
- const history = historyPerEditor . get ( uri )
158
+ const history = historyPerEditor . get ( uri . toString ( ) )
133
159
if ( ! history ) return
134
160
const [ selectedPos ] = history . at ( - 2 ) ?? [ ]
135
161
history . pop ( )
0 commit comments