6
6
import * as vscode from 'vscode' ;
7
7
import { Command } from '../commandManager' ;
8
8
import { MarkdownEngine } from '../markdownEngine' ;
9
- import { TableOfContentsProvider } from '../tableOfContentsProvider' ;
10
- import { isMarkdownFile } from '../util/file' ;
11
- import { extname } from '../util/path' ;
12
-
9
+ import { openDocumentLink } from '../util/openDocumentLink' ;
13
10
14
11
type UriComponents = {
15
12
readonly scheme ?: string ;
@@ -25,11 +22,6 @@ export interface OpenDocumentLinkArgs {
25
22
readonly fromResource : UriComponents ;
26
23
}
27
24
28
- enum OpenMarkdownLinks {
29
- beside = 'beside' ,
30
- currentGroup = 'currentGroup' ,
31
- }
32
-
33
25
export class OpenDocumentLinkCommand implements Command {
34
26
private static readonly id = '_markdown.openDocumentLink' ;
35
27
public readonly id = OpenDocumentLinkCommand . id ;
@@ -60,101 +52,9 @@ export class OpenDocumentLinkCommand implements Command {
60
52
) { }
61
53
62
54
public async execute ( args : OpenDocumentLinkArgs ) {
63
- return OpenDocumentLinkCommand . execute ( this . engine , args ) ;
64
- }
65
-
66
- public static async execute ( engine : MarkdownEngine , args : OpenDocumentLinkArgs ) : Promise < void > {
67
55
const fromResource = vscode . Uri . parse ( '' ) . with ( args . fromResource ) ;
68
- const targetResource = reviveUri ( args . parts ) ;
69
- const column = this . getViewColumn ( fromResource ) ;
70
-
71
- if ( await OpenDocumentLinkCommand . tryNavigateToFragmentInActiveEditor ( engine , targetResource , args ) ) {
72
- return ;
73
- }
74
-
75
- let targetResourceStat : vscode . FileStat | undefined ;
76
- try {
77
- targetResourceStat = await vscode . workspace . fs . stat ( targetResource ) ;
78
- } catch {
79
- // noop
80
- }
81
-
82
- if ( typeof targetResourceStat === 'undefined' ) {
83
- // We don't think the file exists. If it doesn't already have an extension, try tacking on a `.md` and using that instead
84
- if ( extname ( targetResource . path ) === '' ) {
85
- const dotMdResource = targetResource . with ( { path : targetResource . path + '.md' } ) ;
86
- try {
87
- const stat = await vscode . workspace . fs . stat ( dotMdResource ) ;
88
- if ( stat . type === vscode . FileType . File ) {
89
- await OpenDocumentLinkCommand . tryOpenMdFile ( engine , dotMdResource , column , args ) ;
90
- return ;
91
- }
92
- } catch {
93
- // noop
94
- }
95
- }
96
- } else if ( targetResourceStat . type === vscode . FileType . Directory ) {
97
- return vscode . commands . executeCommand ( 'revealInExplorer' , targetResource ) ;
98
- }
99
-
100
- await OpenDocumentLinkCommand . tryOpenMdFile ( engine , targetResource , column , args ) ;
101
- }
102
-
103
- private static async tryOpenMdFile ( engine : MarkdownEngine , resource : vscode . Uri , column : vscode . ViewColumn , args : OpenDocumentLinkArgs ) : Promise < boolean > {
104
- await vscode . commands . executeCommand ( 'vscode.open' , resource , column ) ;
105
- return OpenDocumentLinkCommand . tryNavigateToFragmentInActiveEditor ( engine , resource , args ) ;
106
- }
107
-
108
- private static async tryNavigateToFragmentInActiveEditor ( engine : MarkdownEngine , resource : vscode . Uri , args : OpenDocumentLinkArgs ) : Promise < boolean > {
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
- }
115
- }
116
- this . tryRevealLineUsingLineFragment ( activeEditor , args . fragment ) ;
117
- return true ;
118
- }
119
- return false ;
120
- }
121
-
122
- private static getViewColumn ( resource : vscode . Uri ) : vscode . ViewColumn {
123
- const config = vscode . workspace . getConfiguration ( 'markdown' , resource ) ;
124
- const openLinks = config . get < OpenMarkdownLinks > ( 'links.openLocation' , OpenMarkdownLinks . currentGroup ) ;
125
- switch ( openLinks ) {
126
- case OpenMarkdownLinks . beside :
127
- return vscode . ViewColumn . Beside ;
128
- case OpenMarkdownLinks . currentGroup :
129
- default :
130
- return vscode . ViewColumn . Active ;
131
- }
132
- }
133
-
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 ) ;
152
- editor . selection = new vscode . Selection ( lineStart . start , lineStart . end ) ;
153
- editor . revealRange ( lineStart , vscode . TextEditorRevealType . AtTop ) ;
154
- return true ;
155
- }
156
- }
157
- return false ;
56
+ const targetResource = reviveUri ( args . parts ) . with ( { fragment : args . fragment } ) ;
57
+ return openDocumentLink ( this . engine , targetResource , fromResource ) ;
158
58
}
159
59
}
160
60
@@ -164,36 +64,3 @@ function reviveUri(parts: any) {
164
64
}
165
65
return vscode . Uri . parse ( '' ) . with ( parts ) ;
166
66
}
167
-
168
- export async function resolveLinkToMarkdownFile ( path : string ) : Promise < vscode . Uri | undefined > {
169
- try {
170
- const standardLink = await tryResolveLinkToMarkdownFile ( path ) ;
171
- if ( standardLink ) {
172
- return standardLink ;
173
- }
174
- } catch {
175
- // Noop
176
- }
177
-
178
- // If no extension, try with `.md` extension
179
- if ( extname ( path ) === '' ) {
180
- return tryResolveLinkToMarkdownFile ( path + '.md' ) ;
181
- }
182
-
183
- return undefined ;
184
- }
185
-
186
- async function tryResolveLinkToMarkdownFile ( path : string ) : Promise < vscode . Uri | undefined > {
187
- const resource = vscode . Uri . file ( path ) ;
188
-
189
- let document : vscode . TextDocument ;
190
- try {
191
- document = await vscode . workspace . openTextDocument ( resource ) ;
192
- } catch {
193
- return undefined ;
194
- }
195
- if ( isMarkdownFile ( document ) ) {
196
- return document . uri ;
197
- }
198
- return undefined ;
199
- }
0 commit comments