From 0fd0b390ad5a268c143236566fcdef100f4bfe28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robin=20V=C3=A5geskar?= Date: Tue, 19 Apr 2022 07:59:47 +0200 Subject: [PATCH 1/2] Add new configuration properies and add calculation for aligning with tab --- extension.js | 35 ++++++++++++++++++++++++++++++----- package.json | 15 +++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/extension.js b/extension.js index df22454..8ebbe4d 100644 --- a/extension.js +++ b/extension.js @@ -21,7 +21,11 @@ function activate(context) { aligner.minSeparationLeft = vscode.workspace.getConfiguration().get('autoalign.minSeparationLeft'); aligner.separationRight = vscode.workspace.getConfiguration().get('autoalign.separationRight'); + aligner.minSeparationTabLeft = vscode.workspace.getConfiguration().get('autoalign.minSeparationTabLeft'); + aligner.minSeparationTabRight = vscode.workspace.getConfiguration().get('autoalign.minSeparationTabRight'); aligner.columnWidth = vscode.workspace.getConfiguration().get('autoalign.columnWidth'); + aligner.useTab = vscode.workspace.getConfiguration().get('autoalign.useTabs'); + aligner.tabSize = vscode.workspace.getConfiguration().get('editor.tabSize') || 1; let editor = vscode.window.activeTextEditor; let selections = editor.selections; // handle multiple selections the same @@ -114,7 +118,11 @@ class Aligner { this.skippableEndingItemsArray = []; this.minSeparationLeft = 3; this.minSeparationRight = 1; + this.minSeparationTabLeft = 1; + this.minSeparationTabRight = 0; this.columnWidth = undefined; + this.useTab = false; + this.tabSize = undefined; } align(text) { if (!text) { @@ -148,13 +156,13 @@ class Aligner { let whiteAtEndCount = (unTrimmedLength - trimmendLength); // we need to make sure it is the "minimumLeft" white-space - if (whiteAtEndCount < this.minSeparationLeft) { + if (!this.useTab && whiteAtEndCount < this.minSeparationLeft) { let addAtEndCount = this.minSeparationLeft - whiteAtEndCount; farthestAlignablePosition += addAtEndCount; } // NEAREST FACTOR OF - if (this.columnWidth > 1 && farthestAlignablePosition % this.columnWidth) { + if (!this.useTab && this.columnWidth > 1 && farthestAlignablePosition % this.columnWidth) { let numberOfWholeColumns = ~~(farthestAlignablePosition / this.columnWidth); farthestAlignablePosition = (numberOfWholeColumns + 1) * this.columnWidth; } @@ -183,11 +191,28 @@ class Aligner { // create parts let leftLine = line.substr(0, moveable.position).trimRight(); let rightLine = line.substr(moveable.position + moveable.item.length).trim(); - let leftInsertString = ' '.repeat(position - leftLine.length); - let rightInsertString = ' '.repeat(Math.max(this.minSeparationRight, 1)); - + let leftInsertString = ''; + let rightInsertString = ''; + + if (this.useTab) { + let tabs = this._getTabCount(position, leftLine); + + leftInsertString = '\t'.repeat(tabs + this.minSeparationTabLeft); + rightInsertString = ' ' + '\t'.repeat(this.minSeparationTabRight); + + } else { + leftInsertString = ' '.repeat(position - leftLine.length); + rightInsertString = ' '.repeat(Math.max(this.minSeparationRight, 1)); + } + return leftLine + leftInsertString + moveable.item + rightInsertString + rightLine; } + _getTabCount(position, line) { + let wholeTabs = ~~((position - line.length) / this.tabSize); + let partialTabs = (line.length + (wholeTabs * this.tabSize)) % this.tabSize == 0 ? 0 : 1; + return wholeTabs + partialTabs; + } + _getAlignablePositionAndItem(line) { let reply = { position : -1, diff --git a/package.json b/package.json index a42b0d5..9cff238 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,21 @@ "configuration": [{ "title": "Auto-Align Configuration", "properties": { + "autoalign.useTabs": { + "type": "boolean", + "default": true, + "description": "Use TAB to allign items instead of spaces" + }, + "autoalign.minSeparationTabLeft": { + "type": "number", + "default": 1, + "description": "Minimum number of tabs to the left of the aligned items" + }, + "autoalign.minSeparationTabRight": { + "type": "number", + "default": 0, + "description": "Minimum number of tabs to the right of the aligned items" + }, "autoalign.moveableItems": { "type": "array", "default": [ From b66f31faa968c9f228a217a88ca4d52481b9b89e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robin=20V=C3=A5geskar?= Date: Tue, 19 Apr 2022 09:41:09 +0200 Subject: [PATCH 2/2] Fix bug in _getTabCount --- extension.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/extension.js b/extension.js index 8ebbe4d..e24cb8c 100644 --- a/extension.js +++ b/extension.js @@ -208,8 +208,9 @@ class Aligner { return leftLine + leftInsertString + moveable.item + rightInsertString + rightLine; } _getTabCount(position, line) { - let wholeTabs = ~~((position - line.length) / this.tabSize); - let partialTabs = (line.length + (wholeTabs * this.tabSize)) % this.tabSize == 0 ? 0 : 1; + let closestpos = Math.ceil(position / this.tabSize) * this.tabSize; + let wholeTabs = ~~((closestpos - line.length) / this.tabSize); + let partialTabs = (closestpos - (line.length + (wholeTabs * this.tabSize))) % this.tabSize == 0 ? 0 : 1; return wholeTabs + partialTabs; }