Skip to content

Commit 92e5e57

Browse files
authored
Merge pull request #8 from myaaghubi/v1.5.3
V1.5.3
2 parents df6e207 + be65996 commit 92e5e57

File tree

3 files changed

+101
-21
lines changed

3 files changed

+101
-21
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Change Log
22

3-
## 1.5.2 (latest)
3+
## 1.5.3 (latest)
4+
5+
- Some fixes with lines ending with a comment
6+
- Support the languages with different comment delimiter/syntax
7+
8+
## 1.5.2 (8/6/2024)
49

510
For `for(...;...;...)` statement:
611
- A bug fixed

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "auto-semicolon-vscode",
33
"displayName": "Auto Semicolon",
44
"description": "A handy tool to make programming delicious. All programming languages include both `Semicolon separated` and `Non-Semicolon separated` supported.",
5-
"version": "1.5.2",
5+
"version": "1.5.3",
66
"publisher": "myaaghubi",
77
"license": "GPL-3.0",
88
"icon": "icon.png",
@@ -138,16 +138,16 @@
138138
"test": "node ./out/test/runTest.js"
139139
},
140140
"devDependencies": {
141+
"@types/glob": "^8.1.0",
142+
"@types/mocha": "^10.0.7",
143+
"@types/node": "20.x",
141144
"@types/vscode": "^1.31.0",
142-
"@types/glob": "^10.4.5",
143-
"@types/mocha": "^10.0.1",
144-
"@types/node": "16.x",
145145
"@typescript-eslint/eslint-plugin": "^5.53.0",
146146
"@typescript-eslint/parser": "^5.53.0",
147+
"@vscode/test-electron": "^2.2.3",
147148
"eslint": "^8.34.0",
148-
"glob": "^8.1.0",
149-
"mocha": "^10.2.0",
150-
"typescript": "^4.9.5",
151-
"@vscode/test-electron": "^2.2.3"
149+
"glob": "^10.4.5",
150+
"mocha": "^10.7.3",
151+
"typescript": "^4.9.5"
152152
}
153-
}
153+
}

src/extension.ts

Lines changed: 86 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,33 @@ export function deactivate() { }
3232

3333
type RegExpMatchArrayWithIndex = RegExpMatchArray & { index: number };
3434

35-
// these two variables gonna fill later
35+
// these variables will fill later
3636
let autoSemicolonFormatsIncluded = true;
3737
let autoMoveFormatsIncluded = false;
38+
let commentDelimiter = '//';
39+
40+
41+
type StringDictionary = {
42+
[key: string]: string;
43+
};
44+
45+
const languagesDelimiter:StringDictionary = {
46+
"dotenv": '#',
47+
"dockerfile": '#',
48+
"snippets": '#',
49+
"python": '#',
50+
"ruby": '#',
51+
"perl": '#',
52+
"bash": '#',
53+
"shellscript": '#',
54+
"r": '#',
55+
"julia": '#',
56+
"coffeescript": '#',
57+
"ini": ';',
58+
"bat": ':',
59+
"sql": '%',
60+
"tex": '%',
61+
};
3862

3963
function getConfig() {
4064
return vscode.workspace.getConfiguration('autoSemicolon');
@@ -48,8 +72,18 @@ function isForStatementIgnored() {
4872
return getConfig().supportedLanguageId.ignores.theForStatement;
4973
}
5074

51-
function isUnallowdEndsIncluded(lastCharacter: string) {
75+
function isUnallowdEndsIncluded(lineText: string) {
76+
let pos = lineText.lastIndexOf(commentDelimiter);
77+
78+
// it it commented
79+
if (pos >= 0) {
80+
lineText = lineText.substring(0, pos);
81+
}
82+
83+
let lastCharacter = lineText[lineText.length - 1];
84+
5285
let unallowedEnds = getConfig().autoInsertSemicolon.unallowedEnds.split(",");
86+
5387
if (!Array.isArray(unallowedEnds))
5488
return false;
5589

@@ -110,6 +144,7 @@ function autoSemicolonCommand(editor: vscode.TextEditor, textEdit: vscode.TextEd
110144

111145
autoSemicolonFormatsIncluded = isAutoSemicolonLanguageIdIncluded(languageId);
112146
autoMoveFormatsIncluded = isAutoMoveLanguageIdIncluded(languageId);
147+
commentDelimiter = getCommentDelimiter(languageId);
113148

114149
position = newPosition(line, selection.active.character + 1);
115150

@@ -139,8 +174,8 @@ function autoSemicolonCommand(editor: vscode.TextEditor, textEdit: vscode.TextEd
139174
let length = line.range.end.character + 1;
140175
if (isBetweenTags('{', '}', lineText, currentPos)) {
141176
length = putSemicolonBefore('}', textEdit, selection, line);
142-
} else if (!isUnallowdEndsIncluded(lineText[lineText.length - 1]) || currentPos === line.text.length) {
143-
length = putSemicolonBefore('//', textEdit, selection, line) + 1;
177+
} else if (!isUnallowdEndsIncluded(lineText) || currentPos === line.text.length) {
178+
length = autoSemicolonBeforeComment(textEdit, selection, line) + 1;
144179
}
145180

146181
position = newPosition(line, length);
@@ -165,12 +200,12 @@ function autoSemicolonCommand(editor: vscode.TextEditor, textEdit: vscode.TextEd
165200
});
166201
}
167202

168-
function putSemicolonAfterPos(position: number, textEdit: vscode.TextEditorEdit, selection: vscode.Selection, line: vscode.TextLine, justMove: boolean=false): number {
203+
function putSemicolonAfterPos(position: number, textEdit: vscode.TextEditorEdit, selection: vscode.Selection, line: vscode.TextLine, justMove: boolean = false): number {
169204
position = position >= 0 ? position : 0;
170-
return putSemicolonBefore('//', textEdit, selection, line, justMove) + 1;
205+
return autoSemicolonBeforeComment(textEdit, selection, line, justMove) + 1;
171206
}
172207

173-
function putSemicolonBefore(tag: string, textEdit: vscode.TextEditorEdit, selection: vscode.Selection, line: vscode.TextLine, justMove: boolean=false): number {
208+
function putSemicolonBefore(tag: string, textEdit: vscode.TextEditorEdit, selection: vscode.Selection, line: vscode.TextLine, justMove: boolean = false): number {
174209
let lineText = line.text.trimEnd();
175210
const currentPos = selection.active.character;
176211
const posClose = lineText.indexOf(tag, currentPos);
@@ -180,7 +215,7 @@ function putSemicolonBefore(tag: string, textEdit: vscode.TextEditorEdit, select
180215
const lineTextTrimmed = lineText.substring(0, posClose).trimEnd();
181216
length = lineTextTrimmed.length;
182217

183-
if (!isUnallowdEndsIncluded(lineTextTrimmed[lineTextTrimmed.length - 1]) || currentPos === line.text.length) {
218+
if (!isUnallowdEndsIncluded(lineTextTrimmed) || currentPos === line.text.length) {
184219
lineText = lineText.replace(lineTextTrimmed, lineTextTrimmed + ';');
185220
length += 1;
186221
textEdit.delete(new vscode.Selection(newPosition(line, 0), newPosition(line, line.text.length)));
@@ -196,6 +231,32 @@ function putSemicolonBefore(tag: string, textEdit: vscode.TextEditorEdit, select
196231
return length;
197232
}
198233

234+
function autoSemicolonBeforeComment(textEdit: vscode.TextEditorEdit, selection: vscode.Selection, line: vscode.TextLine, justMove: boolean = false): number {
235+
let lineText = line.text.trimEnd();
236+
const currentPos = selection.active.character;
237+
const posClose = lineText.indexOf(commentDelimiter, currentPos);
238+
let length = lineText.length;
239+
240+
if (posClose >= 0) {
241+
const lineTextTrimmed = lineText.substring(0, posClose).trimEnd();
242+
length = lineTextTrimmed.length;
243+
244+
if (!isUnallowdEndsIncluded(lineTextTrimmed) || currentPos === length) {
245+
lineText = lineText.replace(lineTextTrimmed, lineTextTrimmed + ';');
246+
length += 1;
247+
textEdit.delete(new vscode.Selection(newPosition(line, 0), newPosition(line, line.text.length)));
248+
textEdit.insert(newPosition(line, 0), lineText);
249+
}
250+
251+
return length - commentDelimiter.length + 1;
252+
}
253+
254+
if (!justMove) {
255+
textEdit.insert(newPosition(line, length), ';');
256+
}
257+
return length;
258+
}
259+
199260
function newPosition(line: vscode.TextLine, position: number): vscode.Position {
200261
return new vscode.Position(line.lineNumber, position);
201262
}
@@ -210,17 +271,31 @@ function isBetweenTags(open: string, close: string, lineText: string, currentPos
210271
function isBetweenTagsB(open: string, close: string, lineText: string, currentPos: number): boolean {
211272
const posOpen = lineText.lastIndexOf(open, currentPos);
212273
const posClose = lineText.indexOf(close, currentPos);
213-
274+
214275
return (
215276
posOpen >= 0 && // open tag exists before current position
216277
posOpen < currentPos && // open tag is before current position
217278
posClose >= currentPos // close tag is after current position
218279
);
219280
}
220281

282+
function getCommentDelimiter(languageId: string | undefined): string {
283+
let delimiterDefault = '//';
284+
285+
if (!languageId)
286+
return delimiterDefault;
287+
288+
for (const key in languagesDelimiter) {
289+
if (key == languageId) {
290+
return languagesDelimiter[key];
291+
}
292+
}
293+
294+
return delimiterDefault;
295+
}
296+
221297
function isCommented(lineText: string, currentPos: number): boolean {
222-
const pos = lineText.lastIndexOf('//', currentPos);
223-
return pos >= 0;
298+
return lineText.lastIndexOf(commentDelimiter, currentPos) >= 0;
224299
}
225300

226301
function findTheForStatement(lineText: string, currentPos: number): string[] | null {

0 commit comments

Comments
 (0)