@@ -106,11 +106,15 @@ export class OpenDocumentLinkCommand implements Command {
106
106
}
107
107
108
108
private static async tryNavigateToFragmentInActiveEditor ( engine : MarkdownEngine , resource : vscode . Uri , args : OpenDocumentLinkArgs ) : Promise < boolean > {
109
- if ( vscode . window . activeTextEditor && isMarkdownFile ( vscode . window . activeTextEditor . document ) ) {
110
- if ( vscode . window . activeTextEditor . document . uri . fsPath === resource . fsPath ) {
111
- await this . tryRevealLine ( engine , vscode . window . activeTextEditor , args . fragment ) ;
112
- return true ;
109
+ const activeEditor = vscode . window . activeTextEditor ;
110
+ if ( activeEditor ?. document . uri . fsPath === resource . fsPath ) {
111
+ if ( isMarkdownFile ( activeEditor . document ) ) {
112
+ if ( await this . tryRevealLineUsingTocFragment ( engine , activeEditor , args . fragment ) ) {
113
+ return true ;
114
+ }
113
115
}
116
+ this . tryRevealLineUsingLineFragment ( activeEditor , args . fragment ) ;
117
+ return true ;
114
118
}
115
119
return false ;
116
120
}
@@ -127,25 +131,30 @@ export class OpenDocumentLinkCommand implements Command {
127
131
}
128
132
}
129
133
130
- private static async tryRevealLine ( engine : MarkdownEngine , editor : vscode . TextEditor , fragment ?: string ) {
131
- if ( fragment ) {
132
- const toc = new TableOfContentsProvider ( engine , editor . document ) ;
133
- const entry = await toc . lookup ( fragment ) ;
134
- if ( entry ) {
135
- const lineStart = new vscode . Range ( entry . line , 0 , entry . line , 0 ) ;
134
+ private static async tryRevealLineUsingTocFragment ( engine : MarkdownEngine , editor : vscode . TextEditor , fragment : string ) : Promise < boolean > {
135
+ const toc = new TableOfContentsProvider ( engine , editor . document ) ;
136
+ const entry = await toc . lookup ( fragment ) ;
137
+ if ( entry ) {
138
+ const lineStart = new vscode . Range ( entry . line , 0 , entry . line , 0 ) ;
139
+ editor . selection = new vscode . Selection ( lineStart . start , lineStart . end ) ;
140
+ editor . revealRange ( lineStart , vscode . TextEditorRevealType . AtTop ) ;
141
+ return true ;
142
+ }
143
+ return false ;
144
+ }
145
+
146
+ private static tryRevealLineUsingLineFragment ( editor : vscode . TextEditor , fragment : string ) : boolean {
147
+ const lineNumberFragment = fragment . match ( / ^ L ( \d + ) $ / i) ;
148
+ if ( lineNumberFragment ) {
149
+ const line = + lineNumberFragment [ 1 ] - 1 ;
150
+ if ( ! isNaN ( line ) ) {
151
+ const lineStart = new vscode . Range ( line , 0 , line , 0 ) ;
136
152
editor . selection = new vscode . Selection ( lineStart . start , lineStart . end ) ;
137
- return editor . revealRange ( lineStart , vscode . TextEditorRevealType . AtTop ) ;
138
- }
139
- const lineNumberFragment = fragment . match ( / ^ L ( \d + ) $ / i) ;
140
- if ( lineNumberFragment ) {
141
- const line = + lineNumberFragment [ 1 ] - 1 ;
142
- if ( ! isNaN ( line ) ) {
143
- const lineStart = new vscode . Range ( line , 0 , line , 0 ) ;
144
- editor . selection = new vscode . Selection ( lineStart . start , lineStart . end ) ;
145
- return editor . revealRange ( lineStart , vscode . TextEditorRevealType . AtTop ) ;
146
- }
153
+ editor . revealRange ( lineStart , vscode . TextEditorRevealType . AtTop ) ;
154
+ return true ;
147
155
}
148
156
}
157
+ return false ;
149
158
}
150
159
}
151
160
0 commit comments