-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathReferenceRenameProvider.ts
More file actions
99 lines (79 loc) · 3 KB
/
Copy pathReferenceRenameProvider.ts
File metadata and controls
99 lines (79 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { RenameProvider, TextDocument, Position, Range, WorkspaceEdit, Uri } from 'vscode';
import path from 'path';
import { cache } from '../workspace';
import {
getReferenceAtPosition,
findUriByRef,
isLongRef,
getWorkspaceFolder,
containsMarkdownExt,
containsUnknownExt,
findFilesByExts,
extractExt,
crossPathSort,
} from '../utils';
const openingBracketsLength = 2;
export default class ReferenceRenameProvider implements RenameProvider {
public async prepareRename(
document: TextDocument,
position: Position,
): Promise<Range | { range: Range; placeholder: string }> {
if (document.isDirty) {
throw new Error('Rename is not available for unsaved files. Please save your changes first.');
}
const refAtPos = getReferenceAtPosition(document, position);
if (refAtPos) {
const { range, ref } = refAtPos;
const unknownUris = containsUnknownExt(ref) ? await findFilesByExts([extractExt(ref)]) : [];
const augmentedUris = unknownUris.length
? crossPathSort([...cache.getWorkspaceCache().allUris, ...unknownUris], {
pathKey: 'path',
shallowFirst: true,
})
: cache.getWorkspaceCache().allUris;
if (!findUriByRef(augmentedUris, ref)) {
throw new Error(
'Rename is not available for nonexistent links. Create file first by clicking on the link.',
);
}
return new Range(
new Position(range.start.line, range.start.character + openingBracketsLength),
new Position(range.start.line, range.start.character + openingBracketsLength + ref.length),
);
}
throw new Error('Rename is not available. Please try when focused on the link.');
}
public async provideRenameEdits(
document: TextDocument,
position: Position,
newName: string,
): Promise<WorkspaceEdit> {
const refAtPos = getReferenceAtPosition(document, position);
if (refAtPos) {
const { ref } = refAtPos;
const workspaceEdit = new WorkspaceEdit();
const unknownUris = containsUnknownExt(ref) ? await findFilesByExts([extractExt(ref)]) : [];
const augmentedUris = unknownUris.length
? crossPathSort([...cache.getWorkspaceCache().allUris, ...unknownUris], {
pathKey: 'path',
shallowFirst: true,
})
: cache.getWorkspaceCache().allUris;
const fsPath = findUriByRef(augmentedUris, ref)?.fsPath;
if (fsPath) {
const ext = path.parse(newName).ext;
const newRelativePath = `${newName}${
ext === '' || (containsUnknownExt(newName) && containsMarkdownExt(fsPath)) ? '.md' : ''
}`;
const newUri = Uri.file(
isLongRef(ref)
? path.join(getWorkspaceFolder()!, newRelativePath)
: path.join(path.dirname(fsPath), newRelativePath),
);
workspaceEdit.renameFile(Uri.file(fsPath), newUri);
}
return workspaceEdit;
}
throw new Error('Rename is not available. Please try when focused on the link.');
}
}