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

Add custom delimiter #2

Merged
merged 1 commit into from
Apr 14, 2024
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
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# VSCode Aligner Extension

![VSCode Version](https://img.shields.io/badge/VSCode-1.0.0-blue)
![License](https://img.shields.io/github/license/DungGramer/vs-aligner)

Align your comments effortlessly in Visual Studio Code with the VSCode Comment Aligner Extension. This extension allows you to quickly align comments in your code for improved readability and consistency.
Expand All @@ -9,14 +8,15 @@ Align your comments effortlessly in Visual Studio Code with the VSCode Comment A

- Align comments within a selected range in your code.
- Automatically detects and aligns comments with different lengths.
- Supports single-line comments. (feature update: support multi-line)
- Supports single-line comments.
- Define the character users want to align columns.
- Easily customizable to fit your preferred comment alignment style.

## Installation

1. Launch Visual Studio Code.
2. Go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window.
3. Search for "VSCode Comment Aligner" in the Extensions view search box.
3. Search for [VSCode Comment Aligner](https://marketplace.visualstudio.com/items?itemName=DungGramer.vs-aligner) in the Extensions view search box.
4. Click the Install button to install the extension.
5. Once installed, you can use the extension to align comments in your code.

Expand All @@ -35,6 +35,7 @@ Here are some available configuration options:

- `commentAligner.tabSize`: Set the number of spaces for alignment (default is 4 spaces).
- `commentAligner.alignmentChar`: Choose the character used for alignment (default is space).
- `commentAligner.delimiter`: Define the character users want to align columns.
- And more!
<!-- - `commentAligner.ignorePatterns`: Define patterns to ignore lines that should not be aligned. -->

Expand All @@ -46,10 +47,6 @@ This extension is licensed under the [MIT License](LICENSE).

Contributions are welcome! Feel free to open issues or pull requests on the [GitHub repository](https://github.com/DungGramer/vs-aligner).

## Acknowledgments

Special thanks to the VSCode community and the developers of other comment alignment extensions for inspiration.

## Contact

For any questions, issues, or suggestions, please [open an issue](https://github.com/DungGramer/vs-aligner/issues) on the GitHub repository.
Expand All @@ -58,6 +55,6 @@ For any questions, issues, or suggestions, please [open an issue](https://github

Happy coding!

![DungGramer](https://avatars.githubusercontent.com/DungGramer)
<!-- ![DungGramer](https://avatars.githubusercontent.com/DungGramer) -->

[DungGramer](https://github.com/DungGramer)
Binary file modified images/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 13 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "vs-aligner",
"displayName": "VS Aligner",
"version": "1.0.0",
"version": "1.2.0",
"publisher": "DungGramer",
"description": "VS Aligner is a powerful Visual Studio Code extension that simplifies code alignment and comment formatting. It enhances your coding experience by allowing you to align code comments effortlessly according to your preferred tab size and alignment character.",
"author": {
"name": "DungGramer",
Expand Down Expand Up @@ -43,13 +44,23 @@
"properties": {
"commentAligner.tabSize": {
"type": "integer",
"default": null,
"default": 4,
"description": "Set the number of spaces for alignment (default is user setting)."
},
"commentAligner.alignmentChar": {
"type": "string",
"default": " ",
"description": "Choose the character used for alignment (default is space)."
},
"commentAligner.delimiter": {
"type": "array",
"default": [
"//"
],
"description": "Define the character users want to align columns.",
"items": {
"type": "string"
}
}
}
}
Expand Down
124 changes: 73 additions & 51 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,100 @@
import * as vscode from "vscode";

export function activate(context: vscode.ExtensionContext) {
// console.log('Congratulations, your extension "vs-aligner" is now active!');
const disposable = vscode.commands.registerCommand("vs-aligner.align", () => {
const editor = vscode.window.activeTextEditor;
const document = editor?.document;

const selection = vscode.window.activeTextEditor?.selection;

const startLine = selection?.start.line;
const endLine = selection?.end.line;

const commentInfoArray = [];

if (!endLine || !document || !editor || !startLine) {
if (!editor) {
return;
}
const document = editor.document;
const selection = editor.selection;

for (let line = startLine; line <= endLine; line++) {
const curLineText = document.lineAt(line).text;
const commentInfo = getCommentInfo(curLineText);
const delimiters = vscode.workspace
.getConfiguration("commentAligner")
.get<string[]>("delimiter") || ["//"];
const startLine = selection.start.line;
const endLine = selection.end.line;

commentInfoArray.push(commentInfo);
const lines = [];
for (let line = startLine; line <= endLine; line++) {
lines.push(document.lineAt(line).text);
}

const maxCommentIndex = Math.max(
...commentInfoArray.map((info) => info.commentIndex)
);

const newTextArray = commentInfoArray.map((info) => {
if (info.commentIndex === -1) {
return info.textWithoutComment;
}
// return info.textWithoutComment.padEnd(maxCommentIndex, ' ') + info.comment;
return info.textWithoutComment + addBlankString(maxCommentIndex - info.commentIndex) + info.comment;
});
const alignedLines = alignLines(lines, delimiters);

const newText = newTextArray.join("\n");
const range = new vscode.Range(
startLine,
0,
endLine,
document.lineAt(endLine).text.length
);
// Trim trailing spaces from the last line
alignedLines[alignedLines.length - 1] =
alignedLines[alignedLines.length - 1].trimEnd();

editor.edit((editBuilder) => {
editBuilder.replace(range, newText);
editBuilder.replace(
new vscode.Range(
startLine,
0,
endLine,
document.lineAt(endLine).text.length
),
alignedLines.join("\n")
);
});
});

context.subscriptions.push(disposable);
}

// This method is called when your extension is deactivated
export function deactivate() {}

function addBlankString(num: number) {
const userSetting = vscode.workspace.getConfiguration('editor');
const userTabSize = (userSetting.get('tabSize') || 4) as number;
function alignLines(lines: string[], delimiters: string[]): string[] {
const allParts = lines.map((line) =>
removeBadSpace(parseLine(line, delimiters))
);
const maxPartsLengths: number[] = new Array(allParts[0].length).fill(0);

// Determine the maximum length needed for each segment to align properly
allParts.forEach((parts) => {
parts.forEach((part, index) => {
maxPartsLengths[index] = Math.max(
maxPartsLengths[index],
part.trimEnd().length
);
});
});

const tabSize: undefined | number | null = vscode.workspace.getConfiguration('vs-aligner').get('tabSize');
const alignmentChar = vscode.workspace.getConfiguration('vs-aligner').get('alignmentChar', ' ');
return allParts.map((parts) => {
return parts
.map((part, index) => {
part = part.trimEnd();
const additionalSpace = index < parts.length - 1 ? " " : ""; // Add space between parts except the last part
return (
part +
" ".repeat(maxPartsLengths[index] - part.length) +
additionalSpace
);
})
.join("");
});
}

let blankStr = "";
for (let i = 0; i < num; i++) {
blankStr += alignmentChar.repeat((tabSize || userTabSize) / 2);
}
return blankStr;
function parseLine(lineText: string, delimiters: string[]): string[] {
let parts = [lineText];
delimiters.sort((a, b) => b.length - a.length); // Sort by length to prioritize longer delimiters
delimiters.forEach((delimiter) => {
parts = parts.flatMap((part) => {
const regex = new RegExp(`(${escapeRegExp(delimiter)})`);
return part.split(regex).filter((part) => part !== "");
});
});
return parts;
}

function getCommentInfo(lineText: string) {
const commentIndex = lineText.lastIndexOf("//");
const comment = commentIndex === -1 ? "" : lineText.slice(commentIndex);
const textWithoutComment =
commentIndex === -1 ? lineText : lineText.slice(0, commentIndex);
return { commentIndex, comment, textWithoutComment };
function escapeRegExp(text: string) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}

function removeBadSpace(arr: string[]) {
return arr
.map((part, index) => {
return index > 1 ? part.trim() : part; //TODO: Not trim space first line because can't check this line in block or not
})
.filter(Boolean);
}