Skip to content

Commit

Permalink
add configuration for shaderlabformatter.indentation.conditionMacro
Browse files Browse the repository at this point in the history
  • Loading branch information
litefeel committed Mar 23, 2019
1 parent 9dd1177 commit 4828648
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 18 deletions.
20 changes: 18 additions & 2 deletions package.json
Expand Up @@ -31,7 +31,23 @@
"onLanguage:shaderlab"
],
"main": "./out/extension.js",
"contributes": {},
"contributes": {
"configuration": {
"type": "object",
"title": "ShaderLab Formatter",
"properties": {
"shaderlabformatter.indentation.conditionMacro": {
"enum": [
"dont",
"indent"
],
"type": "string",
"default": "indent",
"description": "indentation for condition macro."
}
}
}
},
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
Expand All @@ -46,4 +62,4 @@
"@types/node": "^10.12.21",
"@types/mocha": "^2.2.42"
}
}
}
54 changes: 38 additions & 16 deletions src/extension.ts
@@ -1,9 +1,6 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import { print } from 'util';
import { ProviderResult } from 'vscode';
import { open } from 'inspector';
import { Indent } from './indent';


Expand All @@ -15,10 +12,17 @@ export function activate(context: vscode.ExtensionContext) {
// This line of code will only be executed once when your extension is activated
// console.log('Congratulations, your extension "shaderlabformatter" is now active!');

enum MacroIndentation {
Dont = "dont",
Indent = "indent",
}

const MACRO_BEGIN = /^\s*#if/;
const MACRO_END = /^\s*#endif/;
const MACRO_MIDDLE = /^\s*(#else|#elif)/;
const BRACKET_LEFT = /\{(?!})/;
const BRACKET_RIGHT = /(?<!{)\}/;

const LEFT_BRACKET = /#if|\{(?!})/;
const RIGHT_BRACKET = /#endif|(?<!{)\}/;
const TEMP_LEFT_BRACKET = /#else|#elif/;
let indentUtil: Indent = new Indent();

function isComment(line: string) {
Expand All @@ -30,6 +34,8 @@ export function activate(context: vscode.ExtensionContext) {
provideDocumentFormattingEdits(document: vscode.TextDocument, options, token) {

indentUtil.initIndent(options.insertSpaces, options.tabSize);
let config = vscode.workspace.getConfiguration("shaderlabformatter.indentation");
let macroIndentation = config.get<MacroIndentation>("conditionMacro", MacroIndentation.Indent);

// vscode.window.showInformationMessage('Hello World!');
const result: vscode.TextEdit[] = [];
Expand All @@ -41,27 +47,43 @@ export function activate(context: vscode.ExtensionContext) {
continue;
}
const lineText = line.text;
const increaseIndent = LEFT_BRACKET.test(lineText);
const decreaseIndent = RIGHT_BRACKET.test(lineText);
const decreaseIndentOnlyOne = TEMP_LEFT_BRACKET.test(lineText);
if (!increaseIndent && decreaseIndent) {
const bracketLeft = BRACKET_LEFT.test(lineText);
const bracketRight = BRACKET_RIGHT.test(lineText);
const macroBegin = MACRO_BEGIN.test(lineText);
const macroEnd = MACRO_END.test(lineText);

const macroMiddle = MACRO_MIDDLE.test(lineText);
if (!bracketLeft && bracketRight) {
indent--;
}
if (decreaseIndentOnlyOne) {
indent--;
let nowIndent = indent;
if (macroEnd || macroMiddle || macroBegin) {
switch (macroIndentation) {
case MacroIndentation.Dont:
nowIndent = 0;
break;
case MacroIndentation.Indent:
if (macroEnd) {
indent--;
nowIndent = indent;
} else if (macroMiddle) {
nowIndent = indent - 1;
}
break;
}
}

var firstCharIdx = line.firstNonWhitespaceCharacterIndex;
if (!indentUtil.isIndent(lineText, firstCharIdx, indent)) {
if (!indentUtil.isIndent(lineText, firstCharIdx, nowIndent)) {
var pos = new vscode.Position(lineIdx, 0);
result.push(vscode.TextEdit.delete(new vscode.Range(lineIdx, 0, lineIdx, firstCharIdx)));
result.push(vscode.TextEdit.insert(line.range.start, indentUtil.getIndent(indent)));
result.push(vscode.TextEdit.insert(line.range.start, indentUtil.getIndent(nowIndent)));
}

if (increaseIndent && !decreaseIndent) {
if (bracketLeft && !bracketRight) {
indent++;
}
if (decreaseIndentOnlyOne) {
if (macroBegin && macroIndentation === MacroIndentation.Indent) {
indent++;
}
}
Expand Down

0 comments on commit 4828648

Please sign in to comment.