Skip to content

Commit

Permalink
Proper line splitting excluding strings
Browse files Browse the repository at this point in the history
- closes #14
  • Loading branch information
mborik committed Jun 1, 2021
1 parent 7fb33d8 commit 7707be4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -278,7 +278,6 @@
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"postinstall": "node ./node_modules/vscode/bin/install",
"test": "npm run compile"
},
"devDependencies": {
Expand Down
37 changes: 31 additions & 6 deletions src/formatProcessor.ts
Expand Up @@ -55,7 +55,18 @@ export class FormatProcessor extends ConfigPropsProvider {

const processFragment = (frag: string): LinePartFrag => {
const [, keyword = frag, rest ] = frag.match(/^(\S+)\s+(.*)$/) || [];
const args = rest?.split(/\s*,\s*/) || [];
const args: string[] = [];
if (typeof rest === 'string') {
if (rest.includes(',')) {
rest.split(/:(?=(?:[^'"]*['"][^'"]*['"])*[^'"]*$)/).forEach((arg, idx) => {
const ret = arg.trimEnd();
args.push(idx ? ret.trimStart() : ret);
});
}
else {
args.push(rest)
}
}
return { keyword, args };
}

Expand Down Expand Up @@ -163,7 +174,18 @@ export class FormatProcessor extends ConfigPropsProvider {
indentLevel = configProps.controlIndent;
lineParts.keyword = keyword.trim();
lineParts.firstParam = firstParam;
lineParts.args = rest ? rest.split(/\s*,\s*/) : [];
lineParts.args = [];
if (typeof rest === 'string') {
if (rest.includes(',')) {
rest.split(/:(?=(?:[^'"]*['"][^'"]*['"])*[^'"]*$)/).forEach((arg, idx) => {
const ret = arg.trimEnd();
lineParts.args?.push(idx ? ret.trimStart() : ret);
});
}
else {
lineParts.args.push(rest)
}
}

text = ''
}
Expand All @@ -176,11 +198,14 @@ export class FormatProcessor extends ConfigPropsProvider {
indentLevel = configProps.baseIndent;
}

const splitLine = text.split(/\s*\:\s*/);
if (configProps.splitInstructionsByColon && splitLine.length > 1) {
lineParts.fragments = splitLine.map(frag => processFragment(frag.trim()));
if (configProps.splitInstructionsByColon && text.includes(':')) {
const splitLine = text.split(/:(?=(?:[^'"]*['"][^'"]*['"])*[^'"]*$)/);
if (splitLine.length > 1) {
lineParts.fragments = splitLine.map(frag => processFragment(frag.trim()));
}
}
else {

if (!lineParts.fragments) {
const { keyword, args } = processFragment(text.trim());
lineParts.keyword = keyword;
lineParts.args = args;
Expand Down

0 comments on commit 7707be4

Please sign in to comment.