Skip to content

Commit

Permalink
feat: allow disabling handling updating files on file renames
Browse files Browse the repository at this point in the history
  • Loading branch information
Princesseuh committed Oct 3, 2024
1 parent 706fba9 commit 301404e
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/language-server/src/plugins/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ export const create = (ts: typeof import('typescript')): LanguageServicePlugin[]
const typeScriptPlugin = plugin.create(context);
return {
...typeScriptPlugin,
async provideFileRenameEdits(oldUri, newUri, token) {
const astroConfig = await context.env.getConfiguration?.<{
updateImportsOnFileMove: { enabled: boolean };
}>('astro');

// Check for `false` explicitly, as the default value is `true`, but it might not be set explicitly depending on the editor
if (astroConfig?.updateImportsOnFileMove.enabled === false) {
return null;
}

return typeScriptPlugin.provideFileRenameEdits!(oldUri, newUri, token);
},
async provideCompletionItems(document, position, completionContext, token) {
const originalCompletions = await typeScriptPlugin.provideCompletionItems!(
document,
Expand Down
3 changes: 3 additions & 0 deletions packages/language-server/test/fixture/renameThis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function sayHello() {
console.log('Hello');
}
3 changes: 3 additions & 0 deletions packages/language-server/test/fixture/renaming.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
import { sayHello } from "./renameThis.js";
---
1 change: 1 addition & 0 deletions packages/language-server/test/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export async function getLanguageServer(): Promise<LanguageServer> {
workspace: {
// Needed for tests that use didChangeWatchedFiles
didChangeWatchedFiles: {},
configuration: true,
},
},
);
Expand Down
67 changes: 67 additions & 0 deletions packages/language-server/test/typescript/renames.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import path from 'node:path';
import { expect } from 'chai';
import type { RenameFilesParams } from 'vscode-languageserver-protocol';
import { WillRenameFilesRequest } from 'vscode-languageserver-protocol';
import { type LanguageServer, getLanguageServer } from '../server.js';

const fixtureDir = path.join(__dirname, '../fixture');

describe('TypeScript - Renaming zzz', async () => {
let languageServer: LanguageServer;

before(async () => (languageServer = await getLanguageServer()));

it('Does not rename imports for files when setting is disabled', async () => {
await languageServer.handle.updateConfiguration({
astro: {
updateImportsOnFileMove: {
enabled: false,
},
},
});

const documentToBeRenamed = await languageServer.handle.openTextDocument(
path.resolve(fixtureDir, 'renameThis.ts'),
'typescript',
);
const newUri = documentToBeRenamed.uri.replace('renameThis.ts', 'renamed.ts');

const edits = await languageServer.handle.connection.sendRequest(WillRenameFilesRequest.type, {
files: [
{
oldUri: documentToBeRenamed.uri,
newUri: newUri,
},
],
} satisfies RenameFilesParams);

expect(edits).to.be.null;
});

it('Renames imports for files when setting is enabled', async () => {
await languageServer.handle.updateConfiguration({
astro: {
updateImportsOnFileMove: {
enabled: true,
},
},
});

const documentToBeRenamed = await languageServer.handle.openTextDocument(
path.resolve(fixtureDir, 'renameThis.ts'),
'typescript',
);
const newUri = documentToBeRenamed.uri.replace('renameThis.ts', 'renamed.ts');

const edits = await languageServer.handle.connection.sendRequest(WillRenameFilesRequest.type, {
files: [
{
oldUri: documentToBeRenamed.uri,
newUri: newUri,
},
],
});

expect(edits).to.not.be.null;
});
});
6 changes: 6 additions & 0 deletions packages/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@
"type": "boolean",
"default": false,
"description": "Enable experimental support for content collection intellisense inside Markdown, MDX and Markdoc. Note that this require also enabling the feature in your Astro config (experimental.contentCollectionIntellisense) (Astro 4.14+)"
},
"astro.updateImportsOnFileMove.enabled": {
"scope": "resource",
"type": "boolean",
"default": false,
"description": "Controls whether the extension updates imports when a file is moved to a new location. In most cases, you'll want to keep this disabled as TypeScript and the Astro TypeScript plugin already handles this for you. Having multiple tools updating imports at the same time can lead to corrupted files."
}
}
},
Expand Down

0 comments on commit 301404e

Please sign in to comment.