Skip to content

Commit

Permalink
Add note severity hashtags #low, #medium, #high
Browse files Browse the repository at this point in the history
Fixes tkrkt#7
  • Loading branch information
canadaduane committed Oct 15, 2020
1 parent 9d27101 commit 33bda58
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 18 deletions.
Binary file added images/gutter-high.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/gutter-low.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/gutter-medium.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
84 changes: 66 additions & 18 deletions src/decorator.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as vscode from "vscode";
import { filterResolved, splitArr } from "./util";
import { getCorrespondingNotes, isNotePath } from "./noteUtil";
import { Severity } from './note';

export class Decorator {
context: vscode.ExtensionContext;
lineDecorator?: vscode.TextEditorDecorationType;
gutterDecorator?: vscode.TextEditorDecorationType;
severity: Severity | undefined;

constructor(context: vscode.ExtensionContext) {
this.context = context;
Expand All @@ -16,14 +18,10 @@ export class Decorator {
if (this.lineDecorator) {
this.lineDecorator.dispose();
}
if (this.gutterDecorator) {
this.gutterDecorator.dispose();
}

const config: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration();

const lineProp: vscode.DecorationRenderOptions = {};
const gutterProp: vscode.DecorationRenderOptions = {};

// set line color
const line: string | undefined = config.get("linenote.lineColor");
Expand All @@ -38,25 +36,62 @@ export class Decorator {
lineProp.overviewRulerColor = ruler.trim();
}


this.lineDecorator = vscode.window.createTextEditorDecorationType(lineProp);
this.maybeReloadGutterDecorator(Severity.Default);
}

maybeReloadGutterDecorator(newSeverity: Severity) {
const gutterProp: vscode.DecorationRenderOptions = {};
const config: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration();

const showGutterIcon: boolean | undefined = config.get(
"linenote.showGutterIcon"
);
if (showGutterIcon) {
let iconPath: string | undefined = config.get("linenote.gutterIconPath");
if (iconPath) {
gutterProp.gutterIconPath = iconPath;
} else {
gutterProp.gutterIconPath = this.context.asAbsolutePath(
"images/gutter.png"
);

if (this.severity === undefined || this.severity !== newSeverity) {
// since we're going to switch decorators, dispose if needed
if (this.gutterDecorator) {
this.gutterDecorator.dispose();
}
gutterProp.gutterIconSize = "cover";
}

this.lineDecorator = vscode.window.createTextEditorDecorationType(lineProp);
this.gutterDecorator = vscode.window.createTextEditorDecorationType(
gutterProp
);
switch (newSeverity) {
case Severity.Low:
gutterProp.gutterIconSize = "cover";
gutterProp.gutterIconPath = this.context.asAbsolutePath(
"images/gutter-low.png"
);
break;
case Severity.Medium:
gutterProp.gutterIconSize = "cover";
gutterProp.gutterIconPath = this.context.asAbsolutePath(
"images/gutter-medium.png"
);
break;
case Severity.High:
gutterProp.gutterIconSize = "cover";
gutterProp.gutterIconPath = this.context.asAbsolutePath(
"images/gutter-high.png"
);
break;
default:
if (showGutterIcon) {
let iconPath: string | undefined = config.get("linenote.gutterIconPath");
if (iconPath) {
gutterProp.gutterIconPath = iconPath;
} else {
gutterProp.gutterIconPath = this.context.asAbsolutePath(
"images/gutter.png"
);
}
gutterProp.gutterIconSize = "cover";
}
}

this.gutterDecorator = vscode.window.createTextEditorDecorationType(
gutterProp
);
}
}

async decorate() {
Expand All @@ -72,6 +107,9 @@ export class Decorator {
return;
}

// if any note has severity marked, use the "most severe" for gutter icon color
let newSeverity = Severity.Default;

// load notes and create decoration options
const notes = await getCorrespondingNotes(fsPath);
const [lineProps, gutterProps] = splitArr(
Expand All @@ -84,6 +122,13 @@ export class Decorator {
await note.readAsMarkdown()
);
markdown.isTrusted = true;

// check if the note has a severity tag in its body
const noteSeverity = await note.readSeverity();
if (noteSeverity > newSeverity) {
newSeverity = noteSeverity;
}

return [
{
range: new vscode.Range(
Expand Down Expand Up @@ -111,6 +156,9 @@ export class Decorator {
return;
}

// reset gutter icon, if necessary
this.maybeReloadGutterDecorator(newSeverity);

editor.setDecorations(this.lineDecorator!, lineProps);
editor.setDecorations(this.gutterDecorator!, gutterProps);
}
Expand Down
24 changes: 24 additions & 0 deletions src/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ export interface Props {
notePath: string;
}

export enum Severity {
Default = 0,
Low = 1,
Medium = 2,
High = 3
}

export class Note implements Props {
fsPath: string;
from: number;
Expand All @@ -26,6 +33,10 @@ export class Note implements Props {
// "[" or "]" or [match, file, from]
static lineLinkMatcher = /\[|\]|(?:([^\s#]*)#L?(\d+)(?:-L?(\d+))?)/g;

// extract note severity from note body
// e.g. #high #medium #low
static severityMatcher = /#(high|medium|low)/i;

constructor(props: Props) {
// e.g. $PROJECT_ROOT/path/to/file.js
this.fsPath = props.fsPath;
Expand Down Expand Up @@ -122,6 +133,19 @@ export class Note implements Props {
return buffer.toString();
}

async readSeverity(): Promise<Severity> {
const found = (await this.read()).match(Note.severityMatcher);
if (found) {
console.log('found', found)
switch (found[0].toLowerCase()) {
case '#low': return Severity.Low;
case '#medium': return Severity.Medium;
case '#high': return Severity.High;
}
}
return Severity.Default;
}

async readAsMarkdown(): Promise<string> {
const [projectRoot] = await getRootFolders(this.fsPath);

Expand Down

0 comments on commit 33bda58

Please sign in to comment.