hover functionality initial move to its own module

This commit is contained in:
Olivier Guinart 2017-09-23 18:04:55 -07:00
parent 2967829f23
commit 6ef863618e
2 changed files with 53 additions and 48 deletions

View File

@ -1,70 +1,27 @@
import * as vscode from 'vscode';
import * as data from '../strings/resources.json';
import * as hover from './hover';
// this method is called when vs code is activated
export function activate(context: vscode.ExtensionContext) {
function initializeOpcodesRegex() {
let r : string = "\\b(";
for (var prop in data) {
if (prop != "--^") {
r += prop + "|";
}
}
// The last '|(--\\^)' group below is the ELUP synomym,
// not considered a word per regex standards, so not in \b boundaries group.
return r.substr(0, r.length - 1) + ")\\b|(--\\^)";
}
let regEx = new RegExp(initializeOpcodesRegex(), 'gi');
// create an empty decorator type (no need for border around the opcode nor different background color)
// this is just to enable hover.
const opcodesDecorationType = vscode.window.createTextEditorDecorationType({
});
hover.initializeOpcodesRegex();
let activeEditor = vscode.window.activeTextEditor;
if (activeEditor) {
triggerUpdateDecorations();
hover.triggerUpdateDecorations(activeEditor);
}
vscode.window.onDidChangeActiveTextEditor(editor => {
activeEditor = editor;
if (editor) {
triggerUpdateDecorations();
hover.triggerUpdateDecorations(activeEditor);
}
}, null, context.subscriptions);
vscode.workspace.onDidChangeTextDocument(event => {
if (activeEditor && event.document === activeEditor.document) {
triggerUpdateDecorations();
hover.triggerUpdateDecorations(activeEditor);
}
}, null, context.subscriptions);
var timeout = null;
function triggerUpdateDecorations() {
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(updateDecorations, 500);
}
function updateDecorations() {
if (!activeEditor) {
return;
}
const text = activeEditor.document.getText();
const opcodes: vscode.DecorationOptions[] = [];
let match;
while (match = regEx.exec(text)) {
const startPos = activeEditor.document.positionAt(match.index);
const endPos = activeEditor.document.positionAt(match.index + match[0].length);
const decoration = { range: new vscode.Range(startPos, endPos), hoverMessage: data[match[0].toUpperCase()] };
opcodes.push(decoration);
}
if (opcodes.length > 0) {
activeEditor.setDecorations(opcodesDecorationType, opcodes);
}
}
}

48
src/hover.ts Normal file
View File

@ -0,0 +1,48 @@
import * as vscode from 'vscode';
import * as data from '../strings/resources.json';
// create an empty decorator type (no need for border around the opcode nor different background color)
// this is just to enable hover.
const opcodesDecorationType = vscode.window.createTextEditorDecorationType({ });
var _regExp: RegExp;
export function initializeOpcodesRegex() {
let r : string = "\\b(";
for (var prop in data) {
if (prop != "--^") {
r += prop + "|";
}
}
// The last '|(--\\^)' group below is the ELUP synomym,
// not considered a word per regex standards, so not in \b boundaries group.
_regExp = new RegExp(r.substr(0, r.length - 1) + ")\\b|(--\\^)", 'gi');
}
let _activeEditor: vscode.TextEditor;
var _timeout = null;
export function triggerUpdateDecorations(activeEditor: vscode.TextEditor) : NodeJS.Timer {
_activeEditor = activeEditor;
if (_timeout) {
clearTimeout(_timeout);
}
_timeout = setTimeout(updateDecorations, 500);
return _timeout;
}
function updateDecorations() {
if (!_activeEditor) {
return;
}
const text = _activeEditor.document.getText();
const opcodes: vscode.DecorationOptions[] = [];
let match;
while (match = _regExp.exec(text)) {
const startPos = _activeEditor.document.positionAt(match.index);
const endPos = _activeEditor.document.positionAt(match.index + match[0].length);
const decoration = { range: new vscode.Range(startPos, endPos), hoverMessage: data[match[0].toUpperCase()] };
opcodes.push(decoration);
}
if (opcodes.length > 0) {
_activeEditor.setDecorations(opcodesDecorationType, opcodes);
}
}