Skip to content

Commit

Permalink
Merge pull request #5 from Heroj04/hover-provider
Browse files Browse the repository at this point in the history
Implement Hover Provider
  • Loading branch information
Heroj04 authored Jan 13, 2022
2 parents 4f272c9 + d3650d1 commit 8adf410
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
82 changes: 82 additions & 0 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,22 @@ const osvar = process.platform;
const evasRepo = "https://github.com/Heroj04/ev-as";
const internalConfigPath = path.join(__dirname, "internalConfig.json")
const internalConfig = fs.existsSync(internalConfigPath) ? JSON.parse(fs.readFileSync(internalConfigPath)) : {}
const argTypes = {
0: "CmdType",
1: "Value",
2: "Work",
3: "Flag",
4: "SysFlag",
5: "String"
}

// Get ev-as from Git
spawn("git", ["-C", evasPath, "pull", evasRepo + ".git"]);
spawn("git", ["clone", evasRepo + ".git", evasPath]);

// Load ev_scripts.json file from ev-as
const ev_scripts = JSON.parse(fs.readFileSync(path.join(evasPath, "ev_scripts.json")))

// Install python requirements for ev-as
if (osvar == "win32") {
spawn("python", ["-m", "pip", "install", "-r", requirements]);
Expand Down Expand Up @@ -223,6 +234,77 @@ function activate(context) {
}
})
}))

// Register Hover Provider Method
context.subscriptions.push(vscode.languages.registerHoverProvider("evscript", {
provideHover(document, position, token) {
// Check Config for Hover Enable
if (!vscode.workspace.getConfiguration("evscript").get("enableHovers")) {
return
}

// Get the word at current position
const range = document.getWordRangeAtPosition(position);
const word = document.getText(range);

// Check for label
if (/^(\w|\\)+:$/.test(document.lineAt(position).text)) {
// Is Label
return new vscode.Hover(`\`\`\`evscript\n(Label) ${word}:\n\`\`\``)
}

// Check for parameter
if (range.start.character > 0) {
let parameter = document.getText(new vscode.Range(range.start.line, range.start.character - 1, range.end.line, range.end.character))
if (/^@\w+$/.test(parameter)) {
// Is Work
return new vscode.Hover(`\`\`\`evscript\n(Work) ${parameter}\n\`\`\``)
} else if (/^\#\w+$/.test(parameter)) {
// Is Flag
return new vscode.Hover(`\`\`\`evscript\n(Flag) ${parameter}\n\`\`\``)
} else if (/^\$\w+$/.test(parameter)) {
// Is SysFlag
return new vscode.Hover(`\`\`\`evscript\n(SysFlag) ${parameter}\n\`\`\``)
}
}


// Check for evCmd
for (const evCmdCode in ev_scripts) {
if (Object.hasOwnProperty.call(ev_scripts, evCmdCode)) {
const evCmd = ev_scripts[evCmdCode];
if (evCmd.name == word) {
// evCmd Found
// Create the method layout
let mainLine = `\`\`\`evscript\n(EvCmd) ${evCmd.name}(`
let descLine = `${evCmd.description}\n\n`
evCmd.validArgs.forEach(arg => {
// For each possible argument
mainLine += arg.name
mainLine += arg.optional ? "?: " : ": "
arg.validArgTypes.forEach(argType => {
// For each arument type an argument can be
mainLine += argTypes[argType] + " | "
});
mainLine = mainLine.substring(0, mainLine.length - 3)
mainLine += ", "
descLine += `@param \`${arg.name}\` - ${arg.description} \n\n`
});
mainLine = evCmd.validArgs.length > 0 ? mainLine.substring(0, mainLine.length - 2) : mainLine
mainLine += ")\n\`\`\`"
return new vscode.Hover([mainLine, descLine])
}
}
// Process Cancellations
if (token.isCancellationRequested) {
return
}
}

// Return nothing found
return
}
}))
}

// this method is called when your extension is deactivated
Expand Down
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"activationEvents": [
"onCommand:evscript.parse",
"onCommand:evscript.assemble",
"onCommand:evscript.assemble.all"
"onCommand:evscript.assemble.all",
"onLanguage:evscript"
],
"main": "./extension.js",
"contributes": {
Expand Down Expand Up @@ -67,8 +68,16 @@
"type": "string",
"default": "",
"description": "Specifies the folder path containing the exported message .json files to use."
},
"evscript.enableHovers": {
"type": "boolean",
"default": true,
"description": "Enable information about EvCmds and Parameters to be displayed on hover."
}
}
},
"capabilities" : {
"hoverProvider" : "true"
}
},
"scripts": {
Expand Down
4 changes: 4 additions & 0 deletions syntaxes/evscript.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
{
"name": "constant.numeric.evscript",
"match": "((?<!@|#|\\$)\\b|(?<=\\W)-)\\d+(\\.\\d+)?\\b"
},
{
"name": "variable.parameter.other.evscript",
"match": "\\w+"
}]
},
"comments": {
Expand Down

0 comments on commit 8adf410

Please sign in to comment.