Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow autoImportFileExcludePatterns to ignore files outside the root #2480

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions packages/language-server/src/ls-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,16 +478,25 @@ export class LSConfigManager {
};
}

getTsUserPreferences(lang: TsUserConfigLang, workspacePath: string | null): ts.UserPreferences {
getTsUserPreferences(
lang: TsUserConfigLang,
normalizedWorkspacePath: string | null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you changed the name here - just for readability, or is there a corresponding normalization missing in another file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just readability because there is a string concatenation for the pattern. It's already normalized since it's from urlToPath the same as what we did in normalizePath.

): ts.UserPreferences {
const userPreferences = this.tsUserPreferences[lang];

if (!workspacePath || !userPreferences.autoImportFileExcludePatterns) {
if (!normalizedWorkspacePath || !userPreferences.autoImportFileExcludePatterns) {
return userPreferences;
}

let autoImportFileExcludePatterns = this.resolvedAutoImportExcludeCache.get(workspacePath);
let autoImportFileExcludePatterns =
this.resolvedAutoImportExcludeCache.get(normalizedWorkspacePath);

if (!autoImportFileExcludePatterns) {
const version = ts.version.split('.');
const major = parseInt(version[0]);
const minor = parseInt(version[1]);

const gte5_4 = major > 5 || (major === 5 && minor >= 4);
autoImportFileExcludePatterns = userPreferences.autoImportFileExcludePatterns.map(
(p) => {
// Normalization rules: https://github.com/microsoft/TypeScript/pull/49578
Expand All @@ -497,17 +506,20 @@ export class LSConfigManager {
return p;
}

return path.join(
workspacePath,
p.startsWith('*')
? '/' + slashNormalized
: isRelative
? p
: '/**/' + slashNormalized
);
// https://github.com/microsoft/vscode/pull/202762
// ts 5.4+ supports leading wildcards
const wildcardPrefix = gte5_4 ? '' : path.parse(normalizedWorkspacePath).root;
return p.startsWith('*')
? wildcardPrefix + slashNormalized
: isRelative
? path.join(normalizedWorkspacePath, p)
: wildcardPrefix + '**/' + slashNormalized;
}
);
this.resolvedAutoImportExcludeCache.set(workspacePath, autoImportFileExcludePatterns);
this.resolvedAutoImportExcludeCache.set(
normalizedWorkspacePath,
autoImportFileExcludePatterns
);
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,14 @@ describe('ts user preferences', function () {
typescript: { ...getPreferences(), ...preferences },
javascript: { ...getPreferences(), ...preferences }
});
return new LSAndTSDocResolver(docManager, [pathToUrl(testFilesDir)], configManager);
return {
lsAndTsDocResolver: new LSAndTSDocResolver(
docManager,
[pathToUrl(testFilesDir)],
configManager
),
configManager
};
}

function getDefaultPreferences(): TsUserPreferencesConfig {
Expand All @@ -79,11 +86,8 @@ describe('ts user preferences', function () {

it('provides auto import completion according to preferences', async () => {
const { docManager, document } = setup('code-action.svelte');
const lsAndTsDocResolver = createLSAndTSDocResolver(docManager);
const completionProvider = new CompletionsProviderImpl(
lsAndTsDocResolver,
new LSConfigManager()
);
const { lsAndTsDocResolver, configManager } = createLSAndTSDocResolver(docManager);
const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver, configManager);

const completions = await completionProvider.getCompletions(
document,
Expand All @@ -102,15 +106,13 @@ describe('ts user preferences', function () {
context: CodeActionContext
) {
const { docManager, document } = setup(filename);
const lsAndTsDocResolver = createLSAndTSDocResolver(docManager);
const completionProvider = new CompletionsProviderImpl(
lsAndTsDocResolver,
new LSConfigManager()
);
const { lsAndTsDocResolver, configManager } = createLSAndTSDocResolver(docManager);
const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver, configManager);

const codeActionProvider = new CodeActionsProviderImpl(
lsAndTsDocResolver,
completionProvider,
new LSConfigManager()
configManager
);

const codeAction = await codeActionProvider.getCodeActions(document, range, context);
Expand All @@ -137,7 +139,7 @@ describe('ts user preferences', function () {

it('provides auto import suggestions according to preferences', async () => {
const { docManager, document } = setup('code-action.svelte');
const lsAndTsDocResolver = createLSAndTSDocResolver(docManager, {
const { lsAndTsDocResolver, configManager } = createLSAndTSDocResolver(docManager, {
suggest: {
autoImports: false,
includeAutomaticOptionalChainCompletions: undefined,
Expand All @@ -147,10 +149,7 @@ describe('ts user preferences', function () {
includeCompletionsWithSnippetText: undefined
}
});
const completionProvider = new CompletionsProviderImpl(
lsAndTsDocResolver,
new LSConfigManager()
);
const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver, configManager);

const completions = await completionProvider.getCompletions(
document,
Expand All @@ -165,14 +164,14 @@ describe('ts user preferences', function () {

function setupImportModuleSpecifierEndingJs() {
const { docManager, document } = setup('module-specifier-js.svelte');
const lsAndTsDocResolver = createLSAndTSDocResolver(docManager, {
const { lsAndTsDocResolver, configManager } = createLSAndTSDocResolver(docManager, {
preferences: {
...getDefaultPreferences(),
importModuleSpecifierEnding: 'js'
}
});

return { document, lsAndTsDocResolver };
return { document, lsAndTsDocResolver, configManager };
}

it('provides auto import for svelte component when importModuleSpecifierEnding is js', async () => {
Expand Down Expand Up @@ -248,16 +247,13 @@ describe('ts user preferences', function () {

async function testExcludeDefinitionDir(pattern: string) {
const { docManager, document } = setup('code-action.svelte');
const lsAndTsDocResolver = createLSAndTSDocResolver(docManager, {
const { lsAndTsDocResolver, configManager } = createLSAndTSDocResolver(docManager, {
preferences: {
...getDefaultPreferences(),
autoImportFileExcludePatterns: [pattern]
}
});
const completionProvider = new CompletionsProviderImpl(
lsAndTsDocResolver,
new LSConfigManager()
);
const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver, configManager);

const completions = await completionProvider.getCompletions(
document,
Expand All @@ -280,4 +276,24 @@ describe('ts user preferences', function () {
it('exclude auto import (**/ pattern)', async () => {
await testExcludeDefinitionDir('**/definition');
});

it('exclude auto import outside of the root', async () => {
const { docManager, document } = setup('code-action-outside-root.svelte');
const { lsAndTsDocResolver, configManager } = createLSAndTSDocResolver(docManager, {
preferences: {
...getDefaultPreferences(),
autoImportFileExcludePatterns: ['definitions.ts']
}
});
const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver, configManager);

const completions = await completionProvider.getCompletions(
document,
Position.create(4, 7)
);

const item = completions?.items.find((item) => item.label === 'blubb');

assert.equal(item, undefined);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<script lang="ts">
import {} from '../definitions'
blu
</script>