Skip to content

Commit

Permalink
Fixed #48
Browse files Browse the repository at this point in the history
Signed-off-by: Max Beckenbauer <[email protected]>
  • Loading branch information
maxbec committed Aug 2, 2020
1 parent 371d248 commit 93cbfae
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 97 deletions.
75 changes: 36 additions & 39 deletions out/extension.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion out/extension.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@
"Formats the files in a multiline-style. E.g. in an items file each part of an item is written in a new line."
]
},
"oh-alignment-tool.multilineIndentAmount": {
"oh-alignment-tool.minimumIndentAmount": {
"type": "number",
"default": 3,
"description": "Tab indent level when formatting multiline items."
"default": 4,
"description": "Minimum separation of thing or item parts."
},
"oh-alignment-tool.enableBetaFeatures": {
"type": "boolean",
Expand Down
81 changes: 39 additions & 42 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,6 @@ function formatItem(item: Item): string {
let config = vscode.workspace.getConfiguration("oh-alignment-tool");
let formatStyle = item.formatOption ? item.formatOption : config.formatStyle;
let newLineAfterItem = config.newLineAfterItem;
let multilineIndentAmount = config.multilineIndentAmount;
let editor = vscode.window.activeTextEditor;
let formattedItem = "";

Expand All @@ -837,14 +836,27 @@ function formatItem(item: Item): string {
}

let highestLengths = [];

highestLengths[0] = utils.generateTabFromSpaces(item.highestLengths[0]);
highestLengths[1] = utils.generateTabFromSpaces(item.highestLengths[1]);
highestLengths[2] = utils.generateTabFromSpaces(item.highestLengths[2]);
highestLengths[3] = utils.generateTabFromSpaces(item.highestLengths[3]);
highestLengths[4] = utils.generateTabFromSpaces(item.highestLengths[4]);
highestLengths[5] = utils.generateTabFromSpaces(item.highestLengths[5]);
highestLengths[6] = utils.generateTabFromSpaces(item.highestLengths[6]);
let indentWithSpaces = editor.options.insertSpaces;
let minIndent = config.minimumIndentAmount;

if (!indentWithSpaces) {
highestLengths[0] = utils.generateTabFromSpaces(item.highestLengths[0]);
highestLengths[1] = utils.generateTabFromSpaces(item.highestLengths[1]);
highestLengths[2] = utils.generateTabFromSpaces(item.highestLengths[2]);
highestLengths[3] = utils.generateTabFromSpaces(item.highestLengths[3]);
highestLengths[4] = utils.generateTabFromSpaces(item.highestLengths[4]);
highestLengths[5] = utils.generateTabFromSpaces(item.highestLengths[5]);
highestLengths[6] = utils.generateTabFromSpaces(item.highestLengths[6]);
} else {
highestLengths[0] = item.highestLengths[0];
highestLengths[0] = item.highestLengths[0];
highestLengths[1] = item.highestLengths[1];
highestLengths[2] = item.highestLengths[2];
highestLengths[3] = item.highestLengths[3];
highestLengths[4] = item.highestLengths[4];
highestLengths[5] = item.highestLengths[5];
highestLengths[6] = item.highestLengths[6];
}

// Check for the formatting style in the user configuration
if (formatStyle === "Column" || formatStyle === "ChannelColumn") {
Expand All @@ -859,16 +871,16 @@ function formatItem(item: Item): string {
// Add the leading whitespace (for group and subgroups)
// Add tabs to string
for (let i = 0; i < item.leadingWhiteSpace; i++) {
newType = "\t" + newType;
newType = editor.options.insertSpaces ? " " + newType : "\t" + newType;
}

if (formatStyle === "ChannelColumn") {
let tabs = "";
let spaces = "";
let tabIndent = highestLengths[0] + highestLengths[1] + highestLengths[2] + highestLengths[3] + highestLengths[4] + highestLengths[5];
let tabIndent = newType.length + newName.length + newLabel.length + newIcon.length + newGroup.length + newTag.length;

for (let i = 0; i < tabIndent; i++) {
tabs = tabs + "\t";
tabs += editor.options.insertSpaces ? " " : "\t";
}

var identResult = item.channel.match(/^\{(\w*)="/);
Expand All @@ -892,41 +904,26 @@ function formatItem(item: Item): string {

// Multiline Format Style
} else if (formatStyle === "Multiline") {
// If item type is longer than the indent, make sure there's at least one space
let typeNameIndent = "";
let tabSize = 0;
let indent = "";

// Get the tab size setting of the current editor
if (editor.options.tabSize !== undefined) {
tabSize = +editor.options.tabSize;
}

// Check if Indent Amount is smaller than item type
if (highestLengths[0] > multilineIndentAmount) {
typeNameIndent = typeNameIndent + "\t";
} else {
let gapSize = multilineIndentAmount - Math.floor(item.type.length / tabSize);
for (let index = 0; index < gapSize; index++) {
typeNameIndent = typeNameIndent + "\t";
}
}
// Fill the required amount of tabs after each item part. For Column Style Formatting
let newMultiType = utils.fillColumns(item.type, highestLengths[0]);

// Check if item parts are empty
let newLabel = utils.fillMultiLines(item.label, multilineIndentAmount, item.leadingWhiteSpace);
let newIcon = utils.fillMultiLines(item.icon, multilineIndentAmount, item.leadingWhiteSpace);
let newGroup = utils.fillMultiLines(item.group, multilineIndentAmount, item.leadingWhiteSpace);
let newTag = utils.fillMultiLines(item.tag, multilineIndentAmount, item.leadingWhiteSpace);
let newChannel = utils.fillMultiLines(item.channel, multilineIndentAmount, item.leadingWhiteSpace);
let newComment = utils.fillMultiLines(item.comment, multilineIndentAmount, item.leadingWhiteSpace);

// Insert a new line after the item if config says so
// Add the leading whitespace (for group and subgroups)
// Add tabs to string
for (let i = 0; i < item.leadingWhiteSpace; i++) {
indent = "\t" + indent;
newMultiType = editor.options.insertSpaces ? " " + newMultiType : "\t" + newMultiType;
}
formattedItem = indent + item.type + typeNameIndent + item.name + newLabel + newIcon + newGroup + newTag + newChannel + newComment;

let newMultiTypeLength = indentWithSpaces ? newMultiType.length : utils.generateTabFromSpaces(newMultiType.length);

// Check if item parts are empty
let newLabel = utils.fillMultiLines(item.label, newMultiTypeLength, item.leadingWhiteSpace);
let newIcon = utils.fillMultiLines(item.icon, newMultiTypeLength, item.leadingWhiteSpace);
let newGroup = utils.fillMultiLines(item.group, newMultiTypeLength, item.leadingWhiteSpace);
let newTag = utils.fillMultiLines(item.tag, newMultiTypeLength, item.leadingWhiteSpace);
let newChannel = utils.fillMultiLines(item.channel, newMultiTypeLength, item.leadingWhiteSpace);
let newComment = utils.fillMultiLines(item.comment, newMultiTypeLength, item.leadingWhiteSpace);

formattedItem = newMultiType + item.name + newLabel + newIcon + newGroup + newTag + newChannel + newComment;
} else {
// @todo add window message for user
return "";
Expand Down
46 changes: 34 additions & 12 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export function fillColumns(str: string, finalLength: number): string {
let tabSize = 0;
let gapLength = 0;
let strLength = 0;
let tab = "";

// Check it item is empty
if (finalLength === 0) {
Expand All @@ -61,13 +62,22 @@ export function fillColumns(str: string, finalLength: number): string {
tabSize = +editor.options.tabSize;
}

// Calculate the width of the column gap
strLength = Math.floor(str.length / tabSize);
gapLength = finalLength - strLength;

// Add tabs to string
for (let i = 0; i < gapLength; i++) {
str = str + "\t";
if (editor.options.insertSpaces) {
for (let e = 0; e < tabSize + finalLength - str.length; e++) {
tab += " ";
}
str += tab;
} else {
tab = "\t";

// Calculate the width of the column gap
strLength = Math.floor(str.length / tabSize);
gapLength = finalLength - strLength;

// Add tabs to string
for (let i = 0; i < gapLength; i++) {
str += tab;
}
}

return str;
Expand All @@ -79,10 +89,11 @@ export function fillColumns(str: string, finalLength: number): string {
* @param str
* @param finalLength
*/
export function fillMultiLines(str: string, indenAmount: number, leadingWhiteSpace: number): string {
export function fillMultiLines(str: string, indentAmount: number, leadingWhiteSpace: number): string {
let editor = vscode.window.activeTextEditor;
let gap = "";
let indent = "";
let tabSize = 0;

// Only execute if there's an active text editor
if (!editor) {
Expand All @@ -93,14 +104,25 @@ export function fillMultiLines(str: string, indenAmount: number, leadingWhiteSpa
return "";
}

// Add tabs to string
for (let i = 0; i < indenAmount; i++) {
gap = gap + "\t";
// Get the tab size setting of the current editor
if (editor.options.tabSize !== undefined) {
tabSize = +editor.options.tabSize;
}

if (editor.options.insertSpaces) {
for (let e = 0; e < indentAmount; e++) {
gap += " ";
}
} else {
// Add tabs to string
for (let i = 0; i < indentAmount; i++) {
gap += "\t";
}
}

// Add tabs to string
for (let i = 0; i < leadingWhiteSpace; i++) {
indent = indent + "\t";
indent += editor.options.insertSpaces ? " " : "\t";
}

str = "\n" + indent + gap + str;
Expand Down

0 comments on commit 93cbfae

Please sign in to comment.