-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextension.ts
124 lines (100 loc) · 3.98 KB
/
extension.ts
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import * as vscode from 'vscode';
import { formatText, FormattingOptions, ITextSection, UnitFormattingType } from './usesFormatter';
import { TextEditor, TextEditorEdit, Range, EndOfLine, TextEditorOptions } from 'vscode';
import { minimatch } from 'minimatch';
interface IUsesFormatterState {
context: vscode.ExtensionContext;
}
function getFormattingOptions(): FormattingOptions {
const configurableSortingArray = vscode.workspace.getConfiguration('pascal-uses-formatter').get('overrideSortingOrder') as string[];
const unitFormattingTypeName = vscode.workspace.getConfiguration('pascal-uses-formatter').get('formattingStyle') as string;
const updateUnitNames = vscode.workspace.getConfiguration('pascal-uses-formatter').get('updateUnitNames') as boolean;
const unitNamesToUpdate = vscode.workspace.getConfiguration('pascal-uses-formatter').get('unitNamesToUpdate') as string[];
const unitFormattingType = unitFormattingTypeName === "Comma at the beginning" ? UnitFormattingType.commaFirst : UnitFormattingType.commaLast;
return {
configurableSortingArray,
unitFormattingType,
updateUnitNames,
unitNamesToUpdate,
};
}
function getLineEnd(eol: EndOfLine): string {
switch (eol) {
case EndOfLine.LF:
return "\n";
case EndOfLine.CRLF:
return "\r\n";
}
return "\n";
}
function getSeparator(options: TextEditorOptions): string {
if (options.insertSpaces) {
if (options.tabSize && (typeof (options.tabSize) === "number")) {
const tabSize = options.tabSize as number;
return " ".repeat(tabSize);
}
" ".repeat(2);
}
return "\t";
}
function shouldExcludeFile(filePath: string): boolean {
const excludePatterns = vscode.workspace.getConfiguration('pascal-uses-formatter').get('excludePatterns') as string[];
return excludePatterns.some(pattern => minimatch(filePath, pattern));
}
function formatDocument(editor: TextEditor, edit: TextEditorEdit) {
const doc = editor.document;
if (shouldExcludeFile(doc.fileName)) {
return;
}
const separator = getSeparator(editor.options);
const lineEnd = getLineEnd(doc.eol);
const text = doc.getText();
const options = getFormattingOptions();
const newSections = formatText(text, separator, lineEnd, options);
if (newSections.length === 0) {
vscode.window.showInformationMessage('pascal-uses-formatter: could not find any uses section.');
}
newSections.forEach((section: ITextSection) => {
const range = new Range(doc.positionAt(section.startOffset), doc.positionAt(section.endOffset));
edit.replace(range, section.value);
});
}
function formatUsesOnCommand(textEditor: TextEditor, edit: TextEditorEdit) {
formatDocument(textEditor, edit);
}
function formatUsesOnSave(e: vscode.TextDocumentWillSaveEvent) {
if (!vscode.window.activeTextEditor) {
vscode.window.showErrorMessage('No active text editor found!');
return;
}
const doFormatOnSave = vscode.workspace.getConfiguration('pascal-uses-formatter').get('formatOnSave');
if (!doFormatOnSave) {
return;
}
const LANGUAGES = ['pascal', 'objectpascal'];
if (!LANGUAGES.includes(e.document.languageId)) {
return;
}
const doBeforeSave = async () => {
const editor = vscode.window.activeTextEditor;
if (!editor) {
return;
}
return editor.edit((edit: TextEditorEdit) => formatUsesOnCommand(editor, edit));
};
e.waitUntil(doBeforeSave());
}
function subscribe(state: IUsesFormatterState) {
vscode.workspace.onWillSaveTextDocument(formatUsesOnSave, state, state.context.subscriptions);
const commandName = "pascal-uses-formatter.formatUses";
const commandDisposable = vscode.commands.registerTextEditorCommand(commandName, formatUsesOnCommand, state);
state.context.subscriptions.push(commandDisposable);
}
export function activate(context: vscode.ExtensionContext) {
console.log('Format uses extension BETA is activated');
let state: IUsesFormatterState = { context };
subscribe(state);
}
export function deactivate() {
console.log('Format uses extension is deactivated');
}