Skip to content

Commit

Permalink
Whole-line Highlighting
Browse files Browse the repository at this point in the history
- Added check for supported language
- Rebuilt decorationOptions to add lineDecorations and wordDecorations
- Added logic to style the line/word with their respective decorations
  • Loading branch information
mpearon committed Jan 20, 2020
1 parent a3db5fd commit 25a691f
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 67 deletions.
86 changes: 59 additions & 27 deletions out/extension.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion out/extension.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 55 additions & 39 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export function activate(context: ExtensionContext) {
// No active edit is open
if (!window.activeTextEditor)
return;
if((window.activeTextEditor.document.languageId) !== 'log'){
return
}
// Display a StatusBar message
window.setStatusBarMessage('Show Trigger Words activated');

Expand All @@ -24,74 +27,87 @@ export function activate(context: ExtensionContext) {
var decorationOptions = [
{
name: 'error',
expression: '(err(or)?|fail(ure)?|crit(ical)?)',
wholeLine: true,
lineDeocoration: {
backgroundColor: "lightred"
expression: 'err(or)?|fail(ure)?|crit(ical)?',
lineDecoration: {
backgroundColor: '#FF000030'
},
matchDecoration: {
color: 'white',
backgroundColor: 'red',
fontWeight: 'bold'
wordDecoration: {
color: 'black',
fontWeight: 'bolder',
backgroundColor: '#FF0000'
}
},
{
name: 'warning',
expression: 'warn(ing)?',
wholeLine: true,
lineDeocoration: {
backgroundColor: "lightorange"
lineDecoration: {
backgroundColor: '#FFFF0030'
},
matchDecoration: {
color: "black",
backgroundColor: "orange",
fontWeight: "bold"
wordDecoration: {
color: 'black',
fontWeight: 'bolder',
backgroundColor: '#FFFF00'
}
},
{
name: 'information',
expression: 'info(rmation)?',
wholeLine: true,
lineDeocoration: {
backgroundColor: "lightblue"
lineDecoration: {
backgroundColor: '#007FFF30'
},
matchDecoration: {
color: "white",
backgroundColor: "blue",
fontWeight: "bold"
wordDecoration: {
color: 'black',
fontWeight: 'bolder',
backgroundColor: '#007FFF'
}
},
{
name: 'success',
expression: 'succe(ssful|eded|ss)',
wholeLine: true,
lineDeocoration: {
backgroundColor: "lightgreen"
lineDecoration: {
backgroundColor: '#00ff0030'
},
matchDecoration: {
color: "white",
backgroundColor: "green",
fontWeight: "bold"
wordDecoration: {
color: 'black',
fontWeight: "bolder",
backgroundColor: '#00ff00'
}
}
]

// Loop through each decoration option
decorationOptions.forEach(option => {
console.log('Show-TriggerWords: Processing ' + option.name);
let optionMatchDecoration = window.createTextEditorDecorationType(option.matchDecoration);
let optionRanges = [];
let optionMatch;
let optionExpression = new RegExp(option.expression, 'gim');
let optionWordDecoration = window.createTextEditorDecorationType(option.wordDecoration);
let optionWordRanges = [];
let optionWord;
let optionLineDecoration = window.createTextEditorDecorationType(option.lineDecoration);
let optionLineRanges = [];
let optionLine;
let optionLineExpression = new RegExp(('.+(' + option.expression + ').+'),'gim');
let optionWordExpression = new RegExp(option.expression,'gim');
if (window.activeTextEditor) {
while (optionMatch = optionExpression.exec(editorText)) {
let optionStartIndex = window.activeTextEditor.document.positionAt(optionMatch.index);
let optionEndIndex = window.activeTextEditor.document.positionAt(optionMatch.index + optionMatch[0].length);
let optionRange = (new Range(optionStartIndex, optionEndIndex));
optionRanges.push(optionRange);
while (optionLine = optionLineExpression.exec(editorText)) {
// Higlight Line
let lineStart = window.activeTextEditor.document.positionAt(optionLine.index);
let lineStop = window.activeTextEditor.document.positionAt(optionLine.index + optionLine[0].length);
let lineRange = (new Range(lineStart, lineStop));
optionLineRanges.push(lineRange);
// Highlight Word
while (optionWord = optionWordExpression.exec(editorText)) {
let wordStart = window.activeTextEditor.document.positionAt(optionWord.index);
let wordStop = window.activeTextEditor.document.positionAt(optionWord.index + optionWord[0].length);
let wordRange = (new Range(wordStart, wordStop));
optionWordRanges.push(wordRange);
continue;

};
continue;
};
window.activeTextEditor.setDecorations(optionMatchDecoration, optionRanges);
// Highlight the line
window.activeTextEditor.setDecorations(optionLineDecoration, optionLineRanges);
// Highlight the match
window.activeTextEditor.setDecorations(optionWordDecoration, optionWordRanges);
};
});
};
Expand Down

0 comments on commit 25a691f

Please sign in to comment.