/
fileRename.ts
35 lines (30 loc) · 1.46 KB
/
fileRename.ts
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
import type * as ts from 'typescript/lib/tsserverlibrary';
import type * as vscode from 'vscode-languageserver-protocol';
import type { TextDocument } from 'vscode-languageserver-textdocument';
import * as shared from '@volar/shared';
import { fileTextChangesToWorkspaceEdit } from './rename';
import type { GetConfiguration } from '../';
import { URI } from 'vscode-uri';
import { getFormatCodeSettings } from '../configs/getFormatCodeSettings';
import { getUserPreferences } from '../configs/getUserPreferences';
export function register(
rootUri: URI,
languageService: ts.LanguageService,
getTextDocument: (uri: string) => TextDocument | undefined,
getConfiguration: GetConfiguration,
) {
return async (oldUri: string, newUri: string): Promise<vscode.WorkspaceEdit | undefined> => {
const document = getTextDocument(oldUri);
const [formatOptions, preferences] = document ? await Promise.all([
getFormatCodeSettings(getConfiguration, document.uri),
getUserPreferences(getConfiguration, document.uri),
]) : [{}, {}];
const fileToRename = shared.getPathOfUri(oldUri);
const newFilePath = shared.getPathOfUri(newUri);
let response: ReturnType<typeof languageService.getEditsForFileRename> | undefined;
try { response = languageService.getEditsForFileRename(fileToRename, newFilePath, formatOptions, preferences); } catch { }
if (!response?.length) return;
const edits = fileTextChangesToWorkspaceEdit(rootUri, response, getTextDocument);
return edits;
};
}