Skip to content

Commit

Permalink
Merge pull request #9 from myaaghubi/v1.5.4
Browse files Browse the repository at this point in the history
V1.5.4
myaaghubi authored Sep 8, 2024
2 parents 92e5e57 + 12647a1 commit 44b79b2
Showing 4 changed files with 99 additions and 54 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Change Log

## 1.5.3 (latest)
## 1.5.4 (latest)

Some fixes & improvements

## 1.5.3 (9/4/2024)

- Some fixes with lines ending with a comment
- Support the languages with different comment delimiter/syntax
48 changes: 33 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -4,33 +4,51 @@
[![release](https://img.shields.io/github/release/myaaghubi/Auto-Semicolon-VSCode.svg?style=for-the-badge&logo=github&logoColor=white&colorA=2b303b&colorB=00e8c6)](https://github.com/myaaghubi/Auto-Semicolon-VSCode/releases/latest)
[![rating](https://img.shields.io/visual-studio-marketplace/stars/myaaghubi.auto-semicolon-vscode?style=for-the-badge&logo=reverbnation&logoColor=white&colorA=2b303b&colorB=FFE66D)](https://marketplace.visualstudio.com/items?itemName=myaaghubi.auto-semicolon-vscode)

A handy tool to make programming delicious.
A useful tool to enhance coding with VSCode.

This extension helps by moving to the end of the line `and/or` putting `;` in the right place.
This extension assists by moving the cursor to the end of the line `and/or` putting the `;` at the end of the line.

All programming languages include both `Semicolon separated` and `Non-Semicolon separated` supported.
It's **`auto`**, **`smart`**, **`handy`**, and **`customizable`**. Don't forget to [🍔 donate](#-donate) if you find joy in it.
All programming languages including both `Semicolon separated` and `Non-Semicolon separated` are supported.
It's **`auto`**, **`smart`**, **`handy`**, and **`customizable`**.

I found nothing for `PHP` so made my own, now it's the `best` for this use.
I found nothing for `PHP` so made my shit, now it's the `best` for all languages.

## How To Use
1. Install it from [market place > auto semicolon](https://marketplace.visualstudio.com/items?itemName=myaaghubi.auto-semicolon-vscode).
2. Use `;` don't need to move your mouse.
You may interested in [🍔 donating](#-donate).

## Install/Use
1. Install it from [market place > auto semicolon](https://marketplace.visualstudio.com/items?itemName=myaaghubi.auto-semicolon-vscode)
2. Press `;` anywhere
Enjoy it

## Notes
- Customizable by settings.
- `Multiple cursors` has been supported.
- Excluding quotes `` "'` `` supported (check it in settings).
- The `for(...;...;)` statement supported (check it in settings).
- `PHP`, `javascript`, `typescript`, `c#`, `c/c++`, `java`, `perl`, `dart`, `swift`, **even** `python`, `go`, `bash`, `scala`, `kotlin`, `r` supported, feel free to add your programming language in the list!

## Shortcuts
- `;` > To have it auto
- `alt + ;` then `;` > To put `;` right in place (not auto)
- `alt + ;` then `;` > To put `;` manual (not auto)

Or use the command palette `ctrl+shift+p` > `Auto Semicolon > Insert At The Position`.
Or use the command palette `ctrl+shift+p` > `Insert Manually`.
- `alt` + twice `;` > To ignore enclosing curly bracket pair `{..}` (force move to the end of the line)

Or use `ctrl+shift+p` > `Auto Semicolon > Auto Insert At The End`.
Or use `ctrl+shift+p` > `Insert Force To The End`.
You can change this option from settings.

## Notes
- `Multiple cursors` has been supported.
- `PHP`, `javascript`, `typescript`, `c#`, `c/c++`, `java`, `perl`, `dart`, `swift`, even `python`, `go`, `bash`, `scala`, `kotlin`, `r` supported, feel free to ask support for your programming language!
## Keybinding
You can customize key bindings, to do that
- Open Command Palette (`ctrl+shift+p`)
- Search for 'Open keyboard shortcuts' then 'Auto Semicolon` > `Change keybinding`
```json
This is the default:
[
{ "key": "alt+; ;", "command": "auto-semicolon-vscode.position-insert-semicolon", "when": "editorTextFocus" },
{ "key": "alt+; ;", "command": "auto-semicolon-vscode.position-insert-semicolon", "when": "editorTextFocus" },
{ "key": "alt+; alt+;", "command": "auto-semicolon-vscode.auto-insert-semicolon-fte", "when": "editorTextFocus" }
]
```

## Preview
![Shot Auto-Semicolon-VSCode](assets/auto-semicolon1.gif)
@@ -40,7 +58,7 @@ I found nothing for `PHP` so made my own, now it's the `best` for this use.
![Shot Auto-Semicolon-VSCode](assets/auto-semicolon3.gif)

## 🍔 Donate
Don't forget to donate if you enjoyed it ☕ 🍺 🍸 🍔
Pick one to donate ☕ 🍺 🍸 🍔

ETH: 0x0ADd51D6855d2DF11BB5F331A3fa345c67a863b2

17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
"name": "auto-semicolon-vscode",
"displayName": "Auto Semicolon",
"description": "A handy tool to make programming delicious. All programming languages include both `Semicolon separated` and `Non-Semicolon separated` supported.",
"version": "1.5.3",
"version": "1.5.4",
"publisher": "myaaghubi",
"license": "GPL-3.0",
"icon": "icon.png",
@@ -20,13 +20,12 @@
"Other"
],
"keywords": [
"auto-semicolon",
"helper",
"keybindings",
"shortcut",
"auto",
"semicolon",
"handy",
"comment",
"auto-semicolon",
"semicolon",
"php",
"perl",
"javascript",
@@ -46,7 +45,9 @@
"bash",
"bat",
"scala",
"kotlin"
"kotlin",
"keybindings",
"shortcut"
],
"activationEvents": [
"onStartupFinished",
@@ -59,7 +60,7 @@
"commands": [
{
"command": "auto-semicolon-vscode.position-insert-semicolon",
"title": "Insert At The Position",
"title": "Insert Manually",
"category": "Auto Semicolon"
},
{
@@ -69,7 +70,7 @@
},
{
"command": "auto-semicolon-vscode.auto-insert-semicolon-fte",
"title": "Auto Insert At The End",
"title": "Insert Force To The End",
"category": "Auto Semicolon"
}
],
82 changes: 52 additions & 30 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@
import * as vscode from 'vscode';

export async function activate(context: vscode.ExtensionContext) {
devMode = context.extensionMode === vscode.ExtensionMode.Development;

let semicolonAtPosition = vscode.commands.registerTextEditorCommand('auto-semicolon-vscode.position-insert-semicolon',
(editor: vscode.TextEditor, textEdit: vscode.TextEditorEdit) => {
return semicolonCommand(editor, textEdit);
@@ -33,16 +35,18 @@ export function deactivate() { }
type RegExpMatchArrayWithIndex = RegExpMatchArray & { index: number };

// these variables will fill later
let devMode = false;
let autoSemicolonFormatsIncluded = true;
let autoMoveFormatsIncluded = false;
let commentDelimiter = '//';
let commentDelimiterIndex = -1;


type StringDictionary = {
[key: string]: string;
[key: string]: string;
};

const languagesDelimiter:StringDictionary = {
const languagesDelimiter: StringDictionary = {
"dotenv": '#',
"dockerfile": '#',
"snippets": '#',
@@ -72,14 +76,7 @@ function isForStatementIgnored() {
return getConfig().supportedLanguageId.ignores.theForStatement;
}

function isUnallowdEndsIncluded(lineText: string) {
let pos = lineText.lastIndexOf(commentDelimiter);

// it it commented
if (pos >= 0) {
lineText = lineText.substring(0, pos);
}

function isUnallowedEndsIncluded(lineText: string) {
let lastCharacter = lineText[lineText.length - 1];

let unallowedEnds = getConfig().autoInsertSemicolon.unallowedEnds.split(",");
@@ -132,6 +129,11 @@ function semicolonCommand(editor: vscode.TextEditor, textEdit: vscode.TextEditor
function autoSemicolonCommand(editor: vscode.TextEditor, textEdit: vscode.TextEditorEdit, forceToEnd: boolean) {
const selections: vscode.Selection[] = [];
const languageId = vscode.window.activeTextEditor?.document.languageId;

autoSemicolonFormatsIncluded = isAutoSemicolonLanguageIdIncluded(languageId);
autoMoveFormatsIncluded = isAutoMoveLanguageIdIncluded(languageId);
commentDelimiter = getCommentDelimiter(languageId);

editor.edit(() => {
try {
editor.selections.forEach((selection) => {
@@ -142,10 +144,7 @@ function autoSemicolonCommand(editor: vscode.TextEditor, textEdit: vscode.TextEd
let position: vscode.Position;
let match;

autoSemicolonFormatsIncluded = isAutoSemicolonLanguageIdIncluded(languageId);
autoMoveFormatsIncluded = isAutoMoveLanguageIdIncluded(languageId);
commentDelimiter = getCommentDelimiter(languageId);

commentDelimiterIndex = indexTagOutOfQuotes(lineText, commentDelimiter);
position = newPosition(line, selection.active.character + 1);

if (lineText.length === 0 || !languageId) {
@@ -154,7 +153,7 @@ function autoSemicolonCommand(editor: vscode.TextEditor, textEdit: vscode.TextEd
} else if (!autoSemicolonFormatsIncluded && !autoMoveFormatsIncluded) {
textEdit.insert(selection.active, ';');

} else if (!forceToEnd && isCommented(lineText, currentPos)) {
} else if (!forceToEnd && commentDelimiterIndex >= 0 && commentDelimiterIndex < currentPos) {
textEdit.insert(selection.active, ';');

} else if (!forceToEnd && isQuotesIgnored() && isInStringLiteral(lineText, currentPos)) {
@@ -174,7 +173,7 @@ function autoSemicolonCommand(editor: vscode.TextEditor, textEdit: vscode.TextEd
let length = line.range.end.character + 1;
if (isBetweenTags('{', '}', lineText, currentPos)) {
length = putSemicolonBefore('}', textEdit, selection, line);
} else if (!isUnallowdEndsIncluded(lineText) || currentPos === line.text.length) {
} else if (!isUnallowedEndsIncluded(lineText) || currentPos === line.text.length) {
length = autoSemicolonBeforeComment(textEdit, selection, line) + 1;
}

@@ -183,17 +182,20 @@ function autoSemicolonCommand(editor: vscode.TextEditor, textEdit: vscode.TextEd
} else {
// just move to the end
let length = line.range.end.character + 1;
if (autoMoveFormatsIncluded) {
length = lineText.substring(0, commentDelimiterIndex).trimEnd().length;
}
position = newPosition(line, length);
}

selection = new vscode.Selection(position, position);
selections.push(selection);
} catch (error) {
// logIt(error.message);
} catch (error: any) {
logIt(error.message);
}
});
} catch (error) {
// logIt(error.message);
} catch (error: any) {
logIt(error.message);
}
}).then(() => {
editor.selections = selections;
@@ -215,7 +217,7 @@ function putSemicolonBefore(tag: string, textEdit: vscode.TextEditorEdit, select
const lineTextTrimmed = lineText.substring(0, posClose).trimEnd();
length = lineTextTrimmed.length;

if (!isUnallowdEndsIncluded(lineTextTrimmed) || currentPos === line.text.length) {
if (!isUnallowedEndsIncluded(lineTextTrimmed) || currentPos === line.text.length) {
lineText = lineText.replace(lineTextTrimmed, lineTextTrimmed + ';');
length += 1;
textEdit.delete(new vscode.Selection(newPosition(line, 0), newPosition(line, line.text.length)));
@@ -234,14 +236,13 @@ function putSemicolonBefore(tag: string, textEdit: vscode.TextEditorEdit, select
function autoSemicolonBeforeComment(textEdit: vscode.TextEditorEdit, selection: vscode.Selection, line: vscode.TextLine, justMove: boolean = false): number {
let lineText = line.text.trimEnd();
const currentPos = selection.active.character;
const posClose = lineText.indexOf(commentDelimiter, currentPos);
let length = lineText.length;

if (posClose >= 0) {
const lineTextTrimmed = lineText.substring(0, posClose).trimEnd();
if (commentDelimiterIndex >= 0) {
const lineTextTrimmed = lineText.substring(0, commentDelimiterIndex).trimEnd();
length = lineTextTrimmed.length;

if (!isUnallowdEndsIncluded(lineTextTrimmed) || currentPos === length) {
if (!isUnallowedEndsIncluded(lineTextTrimmed) || currentPos === length) {
lineText = lineText.replace(lineTextTrimmed, lineTextTrimmed + ';');
length += 1;
textEdit.delete(new vscode.Selection(newPosition(line, 0), newPosition(line, line.text.length)));
@@ -294,8 +295,27 @@ function getCommentDelimiter(languageId: string | undefined): string {
return delimiterDefault;
}

function isCommented(lineText: string, currentPos: number): boolean {
return lineText.lastIndexOf(commentDelimiter, currentPos) >= 0;
function indexTagOutOfQuotes(line: string, tag: string): number {
const regex = new RegExp(`${commentDelimiter}(?!['"\`]\S)`, 'g');
const matches = [];

let match;
while ((match = regex.exec(line)) !== null) {
// Check the preceding characters
const precedingChars = line.slice(0, match.index);
const quoteCount = (precedingChars.match(/['"`]/g) || []).length;

// If the count of quotes is even, add the match
if (quoteCount % 2 == 0) {
matches.push(match);
}
}

if (matches.length == 0) {
return -1;
}

return matches[0].index;
}

function findTheForStatement(lineText: string, currentPos: number): string[] | null {
@@ -338,9 +358,11 @@ function isEmpty(value: string | any[] | null | undefined): boolean {
return (value == null || value.length === 0);
}

async function logIt(message: string | any[] | null | undefined) {
console.log(message);
// vscode.window.showWarningMessage(message?.toString());
async function logIt(message: string | any | any[] | null | undefined) {
if (devMode) {
console.log(message);
// vscode.window.showWarningMessage(message?.toString());
}
}

async function taskChecker(context: vscode.ExtensionContext) {

0 comments on commit 44b79b2

Please sign in to comment.