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

Added configuration to enable absolute paths #8

Merged
merged 1 commit into from
Mar 21, 2018
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
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@
"default": "**/*.{js,jsx,ts,tsx}",
"description": "Glob for files to watch and scan, e.g ./src/** ./src/app/**/*.ts. Defaults to **/*.{ts,tsx}"
},
"autoimport.absolute": {
"type": "boolean",
"default": false,
"description": "True if the imports should be absolute to the workspace root"
},
"autoimport.sourceRoot": {
"type": "string",
"default": "./",
"description": "Change this if your absolute urls are not absolute from workspace root e.g ./src"
},
"autoimport.showNotifications": {
"type": "boolean",
"default": false,
Expand Down
22 changes: 16 additions & 6 deletions src/helpers/path-helper.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as path from 'path';
import * as fs from 'fs';

export class PathHelper {

public static normalisePath(relativePath) {
public static normalisePath(path, absolute) {
let removeFileExtenion = (rp) => {
if (rp) {
rp = rp.substring(0, rp.lastIndexOf('.'))
Expand All @@ -25,14 +26,23 @@ export class PathHelper {
return rp;
}

relativePath = makeRelativePath(relativePath);
relativePath = removeFileExtenion(relativePath);
if (!absolute) {
path = makeRelativePath(path);
}
path = removeFileExtenion(path);

return relativePath;
return path;
}

public static getRelativePath(a, b): string {
return path.relative(path.dirname(a), b);
// Ensure we have a path to a folder
if (fs.lstatSync(a).isFile()) {
a = path.dirname(a);
}
return path.relative(a, b);
}

}
public static joinPaths(a, b): string {
return path.join(a, b);
}
}
16 changes: 3 additions & 13 deletions src/import-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,10 @@ export class ImportAction implements vscode.CodeActionProvider {
}

private actionHandler(context: Context): vscode.Command[] {
let path = (imp: ImportObject) => {
if ((<any>imp.file).discovered) {
return imp.file.fsPath;
} else {
let rp = PathHelper.normalisePath(
PathHelper.getRelativePath(context.document.uri.fsPath, imp.file.fsPath));
return rp;
}
};

let handlers = [];
context.imports.forEach(i => {
context.imports.forEach(imp => {
handlers.push({
title: `Import ${i.name} from ${path(i)}`,
title: `Import ${imp.name} from ${imp.getPath(context.document)}`,
command: 'extension.fixImport',
arguments: [context.document, context.range, context.context, context.token, context.imports]
});
Expand All @@ -101,4 +91,4 @@ export class ImportAction implements vscode.CodeActionProvider {
document, range, context, token
}
}
}
}
22 changes: 3 additions & 19 deletions src/import-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,12 @@ export class ImportCompletion implements vscode.CompletionItemProvider {


private buildCompletionItem(imp: ImportObject, document: vscode.TextDocument): any {

let path = this.createDescription(imp, document);

return {
label: imp.name,
kind: vscode.CompletionItemKind.Reference,
detail: `import from ${path}`,
documentation: `Import ${imp.name} from ${path}`,
detail: `import from ${imp.getPath(document)}`,
documentation: `Import ${imp.name} from ${imp.getPath(document)}`,
command: { title: 'AI: Autocomplete', command: 'extension.resolveImport', arguments: [{ imp, document }] }
}
}

private createDescription(imp: ImportObject, document: vscode.TextDocument) {
let path = (imp: ImportObject) => {
if ((<any>imp.file).discovered) {
return imp.file.fsPath;
} else {
let rp = PathHelper.normalisePath(
PathHelper.getRelativePath(document.uri.fsPath, imp.file.fsPath));
return rp;
}
};
return path(imp);
}
}
}
44 changes: 33 additions & 11 deletions src/import-db.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@

import * as Path from 'path';
import * as vscode from 'vscode';
import { PathHelper } from './helpers/path-helper';

export interface ImportObject {
name: string,
file: vscode.Uri,
isDefault: boolean,

export class ImportObject {

name: string;
file: vscode.Uri;
isDefault: boolean;
discovered: boolean;

constructor(name: string, file: vscode.Uri, isDefault: boolean, discovered: boolean = false) {
this.name = name;
this.file = file;
this.isDefault = isDefault;
this.discovered = discovered;
}

getPath(document: vscode.TextDocument): string {
if (this.discovered) {
return this.file.fsPath;
}
const absolute = vscode.workspace.getConfiguration('autoimport').get<boolean>('absolute');
let basePath = document.uri.fsPath;

if (absolute) {
const sourceRoot = vscode.workspace.getConfiguration('autoimport').get<string>('sourceRoot');
basePath = PathHelper.joinPaths(vscode.workspace.rootPath, sourceRoot);
}
return PathHelper.normalisePath(PathHelper.getRelativePath(basePath, this.file.fsPath), absolute);
}
}


Expand Down Expand Up @@ -42,19 +67,16 @@ export class ImportDb {

}

public static saveImport(name: string, data: any, file: any, isDefault: boolean = false): void {
public static saveImport(name: string, data: any, file: any, isDefault: boolean = false, discovered: boolean): void {

name = name.trim();

if (name === '' || name.length === 1) {
return;
}

let obj: ImportObject = {
name,
file,
isDefault,
}

let obj: ImportObject = new ImportObject(name, file, isDefault, discovered);

let exists = ImportDb.imports.findIndex(m => m.name === obj.name && m.file.fsPath === file.fsPath);

Expand All @@ -63,4 +85,4 @@ export class ImportDb {
}

}
}
}
36 changes: 17 additions & 19 deletions src/import-fixer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,32 @@ export class ImportFixer {

public getTextEdit(document: vscode.TextDocument, imports: Array<ImportObject>) {

let edit: vscode.WorkspaceEdit = new vscode.WorkspaceEdit();
let importObj: vscode.Uri | any = imports[0].file;
let importName: string = imports[0].name;
const edit: vscode.WorkspaceEdit = new vscode.WorkspaceEdit();
const importObj: ImportObject = imports[0];
const path = importObj.getPath(document);

let relativePath = this.normaliseRelativePath(importObj, this.getRelativePath(document, importObj));

if (this.alreadyResolved(document, relativePath, importName)) {
if (this.alreadyResolved(document, path, importObj.name)) {
return edit;
}

if (this.shouldMergeImport(document, relativePath)) {
if (this.shouldMergeImport(document, path)) {
edit.replace(document.uri, new vscode.Range(0, 0, document.lineCount, 0),
this.mergeImports(document, edit, importName, importObj, relativePath));
this.mergeImports(document, edit, importObj.name, importObj.file, path));
} else if (/^\/(\/\*) *@flow/.test(document.getText())) {
edit.insert(document.uri, new vscode.Position(1, 0),
this.createImportStatement(imports[0].name, relativePath, true, imports[0].isDefault));
this.createImportStatement(imports[0].name, path, true, imports[0].isDefault));
} else {
let insertPosition: vscode.Position = document.positionAt(document.getText().lastIndexOf('import')).translate(1, 0);
edit.insert(document.uri, insertPosition,
this.createImportStatement(imports[0].name, relativePath, true, imports[0].isDefault));
this.createImportStatement(imports[0].name, path, true, imports[0].isDefault));
}

return edit;
}

private alreadyResolved(document: vscode.TextDocument, relativePath, importName) {
private alreadyResolved(document: vscode.TextDocument, path, importName) {

let exp = new RegExp('(?:import\ \{)(?:.*)(?:\}\ from\ \')(?:' + relativePath + ')(?:\'\;)')
let exp = new RegExp('(?:import\ \{)(?:.*)(?:\}\ from\ \')(?:' + path + ')(?:\'\;)')

let currentDoc = document.getText();

Expand All @@ -65,20 +63,20 @@ export class ImportFixer {
return false;
}

private shouldMergeImport(document: vscode.TextDocument, relativePath): boolean {
private shouldMergeImport(document: vscode.TextDocument, path): boolean {
let currentDoc = document.getText();

let isCommentLine = (text: string): boolean => {
let firstTwoLetters = text.trim().substr(0, 2);
return firstTwoLetters === '//' || firstTwoLetters === '/*';
}

return currentDoc.indexOf(relativePath) !== -1 && !isCommentLine(currentDoc);
return currentDoc.indexOf(path) !== -1 && !isCommentLine(currentDoc);
}

private mergeImports(document: vscode.TextDocument, edit: vscode.WorkspaceEdit, name, file, relativePath: string) {
private mergeImports(document: vscode.TextDocument, edit: vscode.WorkspaceEdit, name, file, path: string) {

let exp = new RegExp('(?:import\ \{)(?:.*)(?:\}\ from\ \')(?:' + relativePath + ')(?:\'\;)')
let exp = new RegExp('(?:import\ \{)(?:.*)(?:\}\ from\ \')(?:' + path + ')(?:\'\;)')

let currentDoc = document.getText();

Expand All @@ -88,13 +86,13 @@ export class ImportFixer {
let workingString = foundImport[0];

workingString = workingString
.replace(/{|}|from|import|'|"| |;/gi, '').replace(relativePath, '');
.replace(/{|}|from|import|'|"| |;/gi, '').replace(path, '');

let importArray = workingString.split(',');

importArray.push(name)

let newImport = this.createImportStatement(importArray.join(', '), relativePath);
let newImport = this.createImportStatement(importArray.join(', '), path);

currentDoc = currentDoc.replace(exp, newImport);
}
Expand Down Expand Up @@ -153,4 +151,4 @@ export class ImportFixer {

return relativePath;
}
}
}
2 changes: 1 addition & 1 deletion src/import-scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,4 @@ export class ImportScanner {
})
}
}
}
}
2 changes: 1 addition & 1 deletion src/node-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class NodeUpload {
let map = mappings[key];
if (map) {
map.forEach(exp => {
ImportDb.saveImport(exp, exp, { fsPath: key, discovered: true })
ImportDb.saveImport(exp, exp, {fsPath: key}, false, true)
});
}
}
Expand Down