Skip to content

Commit 5fdd7b4

Browse files
authored
Merge pull request #104 from CaptainUnbrauchbar/fix-config-custom-args
fix: 🐛 fixed processing of custom config args and added optional constants parameter to config
2 parents 1638c7f + b6534e3 commit 5fdd7b4

File tree

6 files changed

+41
-8
lines changed

6 files changed

+41
-8
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "answer-set-programming-language-support",
33
"displayName": "Clingo for VSCode",
4-
"version": "1.0.7",
4+
"version": "1.0.8",
55
"description": "Language Support for Clingo ASP (developed at University of Potsdam).",
66
"publisher": "ffrankreiter",
77
"jest": {

sampleConfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
},
1919
"preProcessor": false,
2020
"models": 0,
21+
"constants": ["const1=value1", "const2=value2"],
2122
"customArgs": ""
2223
}
2324
}

schema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"name": { "type": "string" },
77
"author": { "type": "string" },
88
"version": { "type": "string" },
9-
"additionalFile": { "type": "array" },
9+
"additionalFiles": { "type": "array" },
1010
"args": {
1111
"type": "object",
1212
"properties": {
@@ -26,6 +26,7 @@
2626
"timeLimit": { "type": "integer" },
2727
"preProcessor": { "type": "boolean" },
2828
"models": { "type": "integer" },
29+
"constants": { "type": "array" },
2930
"customArgs": { "type": "string" }
3031
}
3132
}

src/configReader.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ function readConfig(setConfig, turnMessagesOff, contextAbsolutePath) {
2727
args.push(...readModels());
2828
args.push(...readCustomArgs());
2929
args.push(...readFiles());
30+
args.push(...readConstants());
3031

3132
if (!turnMessagesOff) {
3233
vscode.window.showInformationMessage(
@@ -42,6 +43,14 @@ function readConfig(setConfig, turnMessagesOff, contextAbsolutePath) {
4243
return args;
4344
}
4445

46+
function readConstants() {
47+
if (jsonConfig.args.constants != undefined) {
48+
return jsonConfig.args.constants.map((constant) => `--const ${constant}`);
49+
} else {
50+
return [];
51+
}
52+
}
53+
4554
/**
4655
* @param {string} contextAbsolutePath
4756
* @param {string} pathToConfig
@@ -127,8 +136,30 @@ function readModels() {
127136

128137
function readCustomArgs() {
129138
if (jsonConfig.args.customArgs != undefined) {
130-
return jsonConfig.args.customArgs.split(" ");
139+
const str = jsonConfig.args.customArgs;
140+
// split custom args into seperate tokens
141+
const tokens = str.match(/(?:[^\s"]+|"[^"]*")+/g) || [];
142+
const result = [];
143+
// process tokens as possible pairs
144+
for (let i = 0; i < tokens.length; i++) {
145+
let token = tokens[i];
146+
if (token.startsWith("-") && i + 1 < tokens.length) {
147+
if (tokens[i + 1].startsWith("-")) {
148+
// next token is another flag, so current token is standalone
149+
result.push(token);
150+
continue;
151+
}
152+
let next = tokens[i + 1];
153+
result.push(`${token} ${next}`);
154+
i++;
155+
} else {
156+
// no possible pair remaining
157+
result.push(token);
158+
}
159+
}
160+
return result;
131161
} else {
162+
// no custom args
132163
return [];
133164
}
134165
}

src/runClingoWasmForFileWithProgress.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ async function runClingoWasmForFileWithProgress(vscode, progress, filePath, mode
2525

2626
fileContent = await fs.promises.readFile(filePath, "utf8");
2727

28-
const additionalFiles = options?.filter((arg) => arg && !arg.startsWith("--")).map((filePath) => filePath.replace(/"/g, ""));
28+
const additionalFiles = options?.filter((arg) => arg && !arg.startsWith("-")).map((filePath) => filePath.replace(/"/g, ""));
2929

3030
if (additionalFiles?.length) {
3131
for (const additionalFilePath of additionalFiles) {
3232
if (!fs.existsSync(additionalFilePath)) {
33-
vscode.window.showErrorMessage(`File not found: ${filePath}`);
33+
vscode.window.showErrorMessage(`File not found: ${additionalFilePath}`);
3434
return null;
3535
}
3636
const additionalContent = await fs.promises.readFile(additionalFilePath, "utf8");
@@ -41,7 +41,7 @@ async function runClingoWasmForFileWithProgress(vscode, progress, filePath, mode
4141
}
4242

4343
// Filter options for Clingo
44-
const clingoOptions = options?.filter((arg) => arg.startsWith("--"));
44+
const clingoOptions = options?.filter((arg) => arg.startsWith("-"));
4545

4646
progress.report({
4747
increment: 50,

0 commit comments

Comments
 (0)