Skip to content

Commit

Permalink
Merge pull request #124 from vanowm/simplify_paths
Browse files Browse the repository at this point in the history
Allow enter relative to aiPath paths
  • Loading branch information
loganch committed Oct 18, 2022
2 parents 64debec + db095cd commit ad6c3eb
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 137 deletions.
58 changes: 25 additions & 33 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"id": "autoit",
"aliases": [
"AutoIt",
"autoit"
"Autoit"
],
"extensions": [
".au3"
Expand Down Expand Up @@ -305,47 +305,47 @@
"autoit.aiPath": {
"type": "string",
"default": "C:\\Program Files (x86)\\AutoIt3\\AutoIt3.exe",
"markdownDescription": "Path to the AutoIt executable, e.g. `C:\\Program Files (x86)\\AutoIt3\\AutoIt3.exe`",
"markdownDescription": "Full path to the AutoIt executable, e.g. `C:\\Program Files (x86)\\AutoIt3\\` or `C:\\Program Files (x86)\\AutoIt3\\AutoIt3.exe`",
"format": "uri"
},
"autoit.wrapperPath": {
"type": "string",
"default": "C:\\Program Files (x86)\\AutoIt3\\SciTE\\AutoIt3Wrapper\\AutoIt3Wrapper.au3",
"markdownDescription": "Path to the AutoIt3Wrapper script, e.g. `C:\\Program Files (x86)\\AutoIt3\\SciTE\\AutoIt3Wrapper\\AutoIt3Wrapper.au3`",
"format": "uri"
"default": "",
"markdownDescription": "Full or relative path to the AutoIt3Wrapper script, e.g. `C:\\Program Files (x86)\\AutoIt3\\SciTE\\AutoIt3Wrapper\\AutoIt3Wrapper.au3`\n\nLeave blank to use default, relative to `#autoit.aiPath#` path."
},
"autoit.checkPath": {
"type": "string",
"default": "C:\\Program Files (x86)\\AutoIt3\\AU3Check.exe",
"markdownDescription": "Path to the AutoIt syntax Checker (Au3Check) executable, e.g. `C:\\Program Files (x86)\\AutoIt3\\AU3Check.exe`",
"format": "uri"
"default": "",
"markdownDescription": "Full or relative path to the AutoIt syntax Checker (Au3Check) executable, e.g. `C:\\Program Files (x86)\\AutoIt3\\AU3Check.exe`\n\nLeave blank to use default, relative to `#autoit.aiPath#` path."
},
"autoit.helpPath": {
"type": "string",
"default": "C:\\Program Files (x86)\\AutoIt3\\AutoIt3Help.exe",
"description": "Path to the AutoIt3Help executable, e.g. `C:\\Program Files (x86)\\AutoIt3\\AutoIt3Help.exe`",
"format": "uri"
"default": "",
"markdownDescription": "Full or relative path to the AutoIt3Help executable, e.g. `C:\\Program Files (x86)\\AutoIt3\\AutoIt3Help.exe`\n\nLeave blank to use default, relative to `#autoit.aiPath#` path."
},
"autoit.infoPath": {
"type": "string",
"default": "",
"markdownDescription": "Full or relative path to the AutoIt Window Info executable, e.g. `C:\\Program Files (x86)\\AutoIt3\\Au3Info.exe`\n\nLeave blank to use default, relative to `#autoit.aiPath#` path."
},
"autoit.kodaPath": {
"type": "string",
"default": "",
"markdownDescription": "Full or relative path to the Koda FormDesigner executable, e.g. `C:\\Program Files (x86)\\AutoIt3\\SciTE\\Koda\\FD.exe`\n\nLeave blank to use default, relative to `#autoit.aiPath#` path."
},
"autoit.smartHelp": {
"type": "array",
"description": "Defines prefixes, paths & sources for additional help files",
"default": [
"",
"",
""
],
"format": "uri"
]
},
"autoit.infoPath": {
"type": "string",
"default": "C:\\Program Files (x86)\\AutoIt3\\Au3Info.exe",
"markdownDescription": "Path to the AutoIt Window Info executable, e.g. `C:\\Program Files (x86)\\AutoIt3\\Au3Info.exe`",
"format": "uri"
},
"autoit.kodaPath": {
"type": "string",
"default": "C:\\Program Files (x86)\\AutoIt3\\SciTE\\Koda\\FD.exe",
"markdownDescription": "Path to the Koda FormDesigner executable, e.g. `C:\\Program Files (x86)\\AutoIt3\\SciTE\\Koda\\FD.exe`",
"autoit.includePaths": {
"type": "array",
"default": [
""
],
"description": "File paths for additional include folders",
"format": "uri"
},
"autoit.showVariablesInGoToSymbol": {
Expand All @@ -358,14 +358,6 @@
"default": true,
"description": "Determines whether regions show up when you use Ctrl+Shift+O to find symbols within a script"
},
"autoit.includePaths": {
"type": "array",
"default": [
"C:\\Program Files (x86)\\AutoIt3\\Include"
],
"description": "File paths for additional include folders",
"format": "uri"
},
"autoit.consoleParams": {
"type": "string",
"default": "",
Expand Down
106 changes: 9 additions & 97 deletions src/ai_commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import { execFile as launch, spawn } from 'child_process';
import path from 'path';
import fs from 'fs';
import { findFilepath, getIncludeText } from './util';
import conf from './ai_config';
const config = conf.config;
conf.addListener(() => runners.cleanup());
import { parse } from "jsonc-parser";
import { commandsList as _commandsList, commandsPrefix} from "./commandsList";
import { decode, encodingExists } from "iconv-lite";
import { showInformationMessage, showErrorMessage, messages } from './ai_showMessage';

const runners = {
list: new Map(), //list of running scripts
Expand Down Expand Up @@ -96,98 +100,6 @@ window.onDidChangeVisibleTextEditors(list => {

});

//accepts new option parameter in second argument: timeout
const {showInformationMessage, showErrorMessage, messages} = (() => {
const messages = { info:{}, error:{} };
const ret = (type) => {
const timers = {};
const func = (...args) => {
let timeout;
const [message, options] = args;
if (options && options instanceof Object && !(options instanceof Array)) {
timeout = options.timeout;
//not sure if we need to bother sanitize options object or not, seems to work as is
// delete options.timeout;
// if (!options.keys().length)
// args.splice(1,1);
}
const _clearTimeout = () =>{
clearTimeout(timers[message]);
delete(timers[message]);
};
_clearTimeout();
let isHidden = false;
const callback = () => {
_clearTimeout();
// https://github.com/microsoft/vscode/issues/153693
for (let i = 0; i < 4; i++) // showing rapidly 4 messages hides the message...an exploit?
window[type].apply(window, args);

isHidden = true;
};
timers[message] = timeout !== undefined && setTimeout(callback, timeout);
return {
get isHidden() {
return isHidden;
},
hide: callback,
message: window[type].apply(window, args).finally(_clearTimeout)
};
};
return func;
};
return {
showInformationMessage: ret("showInformationMessage"),
showErrorMessage: ret("showErrorMessage"),
messages,
};
})();

const config = (() => {
const conf = {
data: workspace.getConfiguration('autoit'),
isCodePage: false
};
const checkEncoding = () =>
{
const value = conf.data.outputCodePage.trim();

conf.isCodePage = value && encodingExists(value);
if (checkEncoding._msg) {
showErrorMessage(checkEncoding._msg, {timeout: 0});
checkEncoding._msg = "";
}

if (value && !conf.isCodePage) {
checkEncoding._msg = `"${value}" code page is not supported`;
showErrorMessage(checkEncoding._msg, { timeout: 30000 });
}
return conf.isCodePage;
};
checkEncoding();
workspace.onDidChangeConfiguration(({ affectsConfiguration }) => {
if (!affectsConfiguration('autoit')) return;

conf.data = workspace.getConfiguration('autoit');
runners.cleanup();
clearTimeout(checkEncoding._timer);
checkEncoding._timer = setTimeout(checkEncoding, 1000);
});
return new Proxy(conf,
{
get(target, prop) {
if (prop == "isCodePage")
return conf.isCodePage;

return target.data[prop];
},
set(target, prop, val) {
if (prop !== "isCodePage")
return target.data.update(prop, val);
}
});
})();

//AutoIt3Wrapper.au3 sets CTRL+Break and CTRL+ALT+Break hotkeys
//they interfere with this extension (unless user changed hotkeys)
//this will disable hotkeys via AutoIt3Wrapper.ini while script is running
Expand Down Expand Up @@ -238,10 +150,10 @@ const aWrapperHotkey = (() => {
clearTimeout(timer);
count.set(id, id);
if (count.size == 1) {
const { ini, data } = fileData();
try {
fs.writeFileSync(ini, data, "utf-8");
} catch (er) { }
const { ini, data } = fileData();
try {
fs.writeFileSync(ini, data, "utf-8");
} catch (er) { }
}
timer = setTimeout(() => this.reset(id), 5000);
return id;
Expand Down Expand Up @@ -436,7 +348,7 @@ let keybindings; //note, we are defining this variable without value!
clearTimeout(settingsTimer);
prefs.update(prefName, prefValue, true);
initKeybindings(profileDir || dir);
};
};

settingsJsonWatcher.onDidChange(settingsJsonWatcherEventHandler);
settingsJsonWatcher.onDidCreate(settingsJsonWatcherEventHandler);
Expand Down
158 changes: 158 additions & 0 deletions src/ai_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@

import { workspace, Uri, FileType} from 'vscode';
import { showInformationMessage, showErrorMessage } from './ai_showMessage';
const conf = {
data: workspace.getConfiguration('autoit'),
paths:{
aiPath: {file: "AutoIt3.exe"},
wrapperPath: {dir: "SciTE\\AutoIt3Wrapper\\", file: "AutoIt3Wrapper.au3"},
checkPath: {file: "AU3Check.exe"},
helpPath: {file: "AutoIt3Help.exe"},
infoPath: {file: "Au3Info.exe"},
kodaPath: {dir: "SciTE\\Koda\\", file: "FD.exe"},
includePaths: [{dir: ""}],
smartHelp: [{dir: "", file: ""}]
}
};

const listeners = new Map();
let listenerId = 0, aiPath = "";
workspace.onDidChangeConfiguration(({ affectsConfiguration }) => {
if (!affectsConfiguration('autoit')) return;

conf.data = workspace.getConfiguration('autoit');
listeners.forEach(listener => {
try{listener();}catch(er){console.error(er);}
});
getPaths();
});

getPaths();

function addListener(listener) {
listeners.set(++listenerId, listener);
return listenerId;
}

function removeListener(id) {
listeners.remove(id);
}

function splitPath(path)
{
path = path.trim().match(/^(.*[\\/])?([^\\/]+)?$/).map(a => a || "");

return {
path: path[0],
dir: path[1] + (path[1] === "" ? "" : "\\"),
file: path[2],
isRelative: !!(path[1] && !path[1].match(/^[a-zA-Z]:[\\/]/))
};
}

function fixPath (key, value, index)
{
const path = splitPath(value || "");
const file = (conf.paths[key][index] || conf.paths[key]).file;
const dir = (conf.paths[key][index] || conf.paths[key]).dir;
if (path.file === "")
path.file = file || "";

if (path.dir === "" || path.isRelative)
path.dir = aiPath.dir + path.dir + (!path.isRelative ? (dir || "") : "");

if (file === undefined)
path.file += "/";

return (path.dir + "/" + path.file).replace(/[\\/]+/g, "\\");
}

function verifyPath (val, obj, key, index)
{
const showError = () =>
{
const timeout = obj.message && !obj.message.isHidden ? 1000 : 0;
if (timeout) {
obj.message.hide();
delete obj.message;
}

if (obj.prevCheck !== val)
{
const type = (conf.paths[key][index] || conf.paths[key]).file !== undefined ? "File" : "Directory";
setTimeout(() => obj.message = showErrorMessage(`${type} "${obj.fullPath}" not found (autoit.${key}${index === undefined ? '' : ` [${index}]`})`), timeout);
}

obj.prevCheck = val;
};

workspace.fs.stat(Uri.parse(`file:///${obj.fullPath}`)).then(data => {
const type = ((conf.paths[key][index] || conf.paths[key]).file !== undefined ? FileType.File : FileType.Directory) | FileType.SymbolicLink;
if (!(data.type & type))
return showError();

if (obj.message)
{
obj.message.hide();
delete obj.message;
}
obj.prevCheck = val;

}).catch(() =>
{
showError();
});
}

function getPaths()
{
aiPath = splitPath(conf.data.aiPath||"");

for(let i in conf.paths)
{
const value = conf.data[i];
if (Array.isArray(value)) {
for(let j = 0; j < value.length; j++ )
{
const val = value[j].trim();
if (conf.paths[i][j] === undefined)
conf.paths[i][j] = Object.assign({fullPath: ""}, conf.paths[i][0]);

const obj = conf.paths[i][j];

if (val !== "")
obj.fullPath = fixPath(i, val, j);

if (obj.fullPath === undefined)
obj.fullPath = "";

verifyPath(val, obj, i, j);
}
}
else {
const obj = conf.paths[i];
obj.fullPath = fixPath(i, value);
verifyPath(value, obj, i);
}

}
}
const config = new Proxy(conf,
{
get(target, prop) {
if (target.paths[prop]) {
if (Array.isArray(target.paths[prop]))
return target.paths[prop].map(a => a.fullPath);

return target.paths[prop].fullPath;
}
return target.data[prop];
},
set(target, prop, val) { return target.data.update(prop, val); }
});

export default {
config,
addListener,
removeListener
};
Loading

0 comments on commit ad6c3eb

Please sign in to comment.