Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a792e19
commit fdd696c
Showing
8 changed files
with
209 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { | ||
Diagnostic, | ||
DiagnosticCollection, | ||
DiagnosticSeverity, | ||
} from 'vscode'; | ||
import Linker from "./linker"; | ||
import { Disposable } from "./util/disposable"; | ||
import { Link } from './util/link'; | ||
|
||
class Issue extends Diagnostic { | ||
constructor(public link: Link, message: string, severity: DiagnosticSeverity) { | ||
super(link.range, message, severity); | ||
} | ||
} | ||
|
||
export class MarkdownDiagnosticHandler extends Disposable { | ||
|
||
constructor(diagnostics: DiagnosticCollection, linker: Linker) { | ||
super(); | ||
this.register(linker.onUpdatedLinks(async uri => { | ||
// First: trash ALL diagnostics for this link text. | ||
const issues: Issue[] = []; | ||
for (let link of (await linker.linksIn(uri)) ?? []) { | ||
const links = linker.linksFor(link.linkId.text) ?? []; | ||
this.checkMultihead(issues, links, link) && | ||
this.checkHeadless(issues, links, link) && | ||
this.checkWeak(issues, links, link); | ||
} | ||
diagnostics.set(uri, issues); | ||
})); | ||
} | ||
|
||
private checkMultihead(issues: Issue[], links: Link[], link: Link): Boolean { | ||
if (links?.filter(l => l.isHead).length > 1) { | ||
issues.push(new Issue(link, "Multiple # heads for this link", DiagnosticSeverity.Error)); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
private checkHeadless(issues: Issue[], links: Link[], link: Link): Boolean { | ||
if (!link.isHead && !links?.find(l => l.isHead)) { | ||
issues.push(new Issue(link, "No # head for this link", DiagnosticSeverity.Warning)); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
private checkWeak(issues: Issue[], links: Link[], link: Link): Boolean { | ||
if (links.length < 2) { | ||
issues.push(new Issue(link, "No links to this; make more connections", DiagnosticSeverity.Hint)); | ||
return false; | ||
} | ||
if (links.length < 3) { | ||
issues.push(new Issue(link, "Only one other link to this; make more connections", DiagnosticSeverity.Hint)); | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as assert from 'assert'; | ||
import * as vscode from 'vscode'; | ||
import * as path from 'path'; | ||
import { | ||
TextEditor, | ||
Position, | ||
Range, | ||
Selection | ||
} from 'vscode'; | ||
|
||
suite('link command', async () => { | ||
const testDirectory = '../../../src/test/res/'; | ||
const uri = vscode.Uri.file(path.join(__dirname + testDirectory + 'test.md')); | ||
|
||
async function setText(editor: TextEditor, text: string) { | ||
await editor.edit(builder => { | ||
builder.delete(new Range(new Position(0, 0), | ||
editor.document.lineAt(editor.document.lineCount - 1).range.end)); | ||
builder.insert(new Position(0, 0), text); | ||
}); | ||
} | ||
test('link a heading', async () => { | ||
// Open the document | ||
const document = await vscode.workspace.openTextDocument(uri); | ||
const editor = await vscode.window.showTextDocument(document); | ||
await setText(editor, '# Hello\n\nSome text'); | ||
editor.selection = new Selection(new Position(0, 0), new Position(0, 0)); | ||
|
||
// Insert a link on the top line | ||
await vscode.commands.executeCommand('linkist.link'); | ||
|
||
// Validate there's a link there | ||
assert.ok(editor.document.lineAt(0).text.match(/\# \[Hello\]\(\^[A-Za-z0-9]{4,7}\^\)/)); | ||
}); | ||
test('link a bullet', async () => { | ||
// Open the document | ||
const document = await vscode.workspace.openTextDocument(uri); | ||
const editor = await vscode.window.showTextDocument(document); | ||
await setText(editor, '* Hello\n\nSome text'); | ||
editor.selection = new Selection(new Position(0, 0), new Position(0, 0)); | ||
|
||
// Link the line | ||
await vscode.commands.executeCommand('linkist.link'); | ||
|
||
// Validate there's a link there | ||
assert.ok(editor.document.lineAt(0).text.match(/\* \[Hello\]\(\^[A-Za-z0-9]{4,7}\^\)/)); | ||
}); | ||
}); |