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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
const vscode = require('vscode');
const { getModuleName, getImportsRange, getFileContent } = require('./utils');
const BUILTIN_TYPE = "builtin"
const THIRD_PARTY_TYPE = "thirdParty"
const VENDOR_TYPE = "vendor"
const LOCAL_TYPE = "local"
function getImports(fileContent) {
try {
let importsRegex = new RegExp("(?<=import \\(\n).*?(?=\\))", "s")
let imports = importsRegex.exec(fileContent)[0]
return imports
} catch(_) {
vscode.window.showErrorMessage("Could not find imports")
}
}
function convertImportsToList(imports) {
return imports.split("\n")
}
function getImportType(import_) {
let goModName = getModuleName() + "/"
let vendorPath = goModName.substring(0, goModName.lastIndexOf('/'));
let isBuiltin = !import_.includes(".") && !import_.includes(goModName)
let isThirdParty = import_.includes(".") && !import_.includes(goModName)
let isLocal = import_.includes(goModName)
let isVendor = import_.includes(vendorPath) && !isLocal
if(isBuiltin) {
return BUILTIN_TYPE
} else if (isVendor) {
return VENDOR_TYPE
} else if(isThirdParty) {
return THIRD_PARTY_TYPE
} else if(isLocal) {
return LOCAL_TYPE
}
}
function importGroupsToString(importsGroup) {
return Object.keys(importsGroup)
.filter((key) => importsGroup[key].length)
.map((key) => importsGroup[key].join('\n'))
.join('\n\n');
}
function saveImportsGroup(importsGroup, importRanges, activeEditor) {
const edit = new vscode.WorkspaceEdit();
const range = new vscode.Range(
importRanges.start,
0,
importRanges.end - 1,
Number.MAX_VALUE
);
let importsParsed = importGroupsToString(importsGroup)
let documentUri = activeEditor.document.uri
edit.replace(documentUri, range, importsParsed);
vscode.workspace.applyEdit(edit).then(activeEditor.document.save);
}
function getImportGroups(importsList) {
const importsGroup = {
[BUILTIN_TYPE]: [],
[THIRD_PARTY_TYPE]: [],
[VENDOR_TYPE]: [],
[LOCAL_TYPE]: [],
};
importsList.filter(n => n).forEach(import_ => {
let importType = getImportType(import_)
importsGroup[importType].push(import_)
})
return importsGroup
}
function formatImports() {
const activeEditor = vscode.window.activeTextEditor
let fileContent = getFileContent(activeEditor)
let imports = getImports(fileContent)
let importsList = convertImportsToList(imports)
let importsGroup = getImportGroups(importsList)
let importsRange = getImportsRange(fileContent)
saveImportsGroup(importsGroup,importsRange,activeEditor)
}
module.exports = formatImports;
|