Skip to content

Insert import statement from codefix #5

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

Merged
merged 1 commit into from
Sep 5, 2021
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
13 changes: 13 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ function init() {

return namespaceImportPlugin.getCompletionEntryDetails(name, fileName, data.modulePath, info.project);
};

const getCodeFixesAtPosition = info.languageService.getCodeFixesAtPosition;
info.languageService.getCodeFixesAtPosition = (fileName, start, end, errorCodes, formatOptions, preferences) => {
log('getCodeFixesAtPosition', { fileName, start, end, errorCodes, formatOptions, preferences });
const original = getCodeFixesAtPosition(fileName, start, end, errorCodes, formatOptions, preferences);

const importAction = namespaceImportPlugin.getCodeFixActionByName(fileName, start, end, info);
if (importAction) {
return [importAction, ...original];
}

return original;
};
}

return { create };
Expand Down
86 changes: 59 additions & 27 deletions src/lib/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ type PluginOptions = {
};

export function getCompletionEntries(info: ts.server.PluginCreateInfo): ts.CompletionEntry[] {
const options = info.config.options as PluginOptions;

const currentDir = info.project.getCurrentDirectory();
const filePaths = options.paths.flatMap((dirPath) => {
return info.project.readDirectory(path.resolve(currentDir, dirPath), ['.ts', '.js']);
});
const filePaths = getPathsToImport(info.config.options, info.project);

return filePaths.map((filePath) => {
const name = getFileNameWithoutExt(filePath);
Expand All @@ -36,27 +31,7 @@ export function getCompletionEntryDetails(
modulePath: string,
project: ts.server.Project,
): ts.CompletionEntryDetails {
const importPath = transformModulePath(selfPath, modulePath, project);
const text = `import * as ${name} from "${getFilePathWithoutExt(importPath)}";\n`;
const action: CodeFixAction = {
fixName: 'namespace-import',
description: text,
changes: [
{
fileName: selfPath,
textChanges: [
{
span: {
start: 0,
length: 0,
},
newText: text,
},
],
},
],
commands: [],
};
const action: CodeFixAction = getCodeFixActionFromPath(name, selfPath, modulePath, project);
return {
name: name,
kind: ScriptElementKind.alias,
Expand All @@ -66,6 +41,34 @@ export function getCompletionEntryDetails(
};
}

export function getCodeFixActionByName(
selfPath: string,
start: number,
end: number,
info: ts.server.PluginCreateInfo,
): CodeFixAction | null {
const name = info.languageService.getProgram()?.getSourceFile(selfPath)?.text.slice(start, end);
if (!name) {
return null;
}

const filePaths = getPathsToImport(info.config.options, info.project);
const modulePath = filePaths.find((filePath) => getFileNameWithoutExt(filePath) === name);
if (modulePath) {
return getCodeFixActionFromPath(name, selfPath, modulePath, info.project);
} else {
return null;
}
}

function getPathsToImport(options: PluginOptions, project: ts.server.Project): string[] {
const currentDir = project.getCurrentDirectory();

return options.paths.flatMap((dirPath) => {
return project.readDirectory(path.resolve(currentDir, dirPath), ['.ts', '.js']);
});
}

function getFileNameWithoutExt(filePath: string): string {
const ext = path.extname(filePath);
return path.basename(filePath, ext);
Expand All @@ -84,3 +87,32 @@ function transformModulePath(selfPath: string, filePath: string, project: ts.ser
return './' + path.relative(path.dirname(selfPath), filePath);
}
}

export function getCodeFixActionFromPath(
name: string,
selfPath: string,
modulePath: string,
project: ts.server.Project,
): CodeFixAction {
const importPath = transformModulePath(selfPath, modulePath, project);
const text = `import * as ${name} from "${getFilePathWithoutExt(importPath)}";\n`;
return {
fixName: 'namespace-import',
description: text,
changes: [
{
fileName: selfPath,
textChanges: [
{
span: {
start: 0,
length: 0,
},
newText: text,
},
],
},
],
commands: [],
};
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy