Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Note severity #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ Notes are saved in `$PROJECT_ROOT/.vscode/linenote` like `.vscode/linenote/path/

![example](https://i.imgur.com/KlQtCsL.gif)

Tips: You can put hyperlinks in notes by writing `#L42` or `../foo.js#L12-L15`.
## Tips

- You can put hyperlinks in notes by writing `#L42` or `../foo.js#L12-L15`.
- You can change the severity/color of the note with #low, #medium, #high tags
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) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is the right behavior. I can see two possibilities:

  • the showGutterIcon setting should be absolute & final, i.e. NO gutter icons whatsoever
  • OR the showGutterIcon should be for default icons. If the user adds severity hashtags in the note, the presence of the tag overrides the showGutterIcon setting.

I've implemented the latter.

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