This repository has been archived by the owner on Jul 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.ts
151 lines (127 loc) · 4.79 KB
/
extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import * as path from "path";
import {
commands,
Extension,
ExtensionContext,
extensions,
OutputChannel,
Position,
Range,
Selection,
TextEditorRevealType,
Uri,
ViewColumn,
window,
workspace,
} from "vscode";
import { DocumentContentProvider, isMarkdownFile } from "./provider";
import { MarkdocsServer } from "./server";
import * as util from "./util/common";
import { Logger } from "./util/logger";
let channel: OutputChannel = null;
let server: MarkdocsServer = null;
export async function activate(context: ExtensionContext) {
const extensionId = "qezhu.markdocs";
const extension = extensions.getExtension(extensionId);
util.setExtensionPath(extension.extensionPath);
channel = window.createOutputChannel("markdocs");
const logger = new Logger((text) => channel.append(text));
server = new MarkdocsServer(context);
const provider = new DocumentContentProvider(context);
await server.ensureRuntimeDependencies(extension, channel, logger);
await server.startMarkdocsServerAsync();
const registration = workspace.registerTextDocumentContentProvider(DocumentContentProvider.scheme, provider);
const disposableSidePreview = commands.registerCommand("markdocs.showPreviewToSide", (uri) => {
preview(uri, ViewColumn.Two, provider);
});
const disposableStandalonePreview = commands.registerCommand("markdocs.showPreview", (uri) => {
preview(uri, ViewColumn.One, provider);
});
const disposableDidClick = commands.registerCommand("markdocs.didClick", (uri, line) => {
click(uri, line);
});
const disposableRevealLink = commands.registerCommand("markdocs.revealLine", (uri, line) => {
reveal(uri, line);
});
context.subscriptions.push(
disposableSidePreview,
disposableStandalonePreview,
disposableDidClick,
disposableRevealLink,
registration);
context.subscriptions.push(workspace.onDidChangeTextDocument((event) => {
if (isMarkdownFile(event.document)) {
const uri = getPreviewUri(event.document.uri);
provider.update(uri);
}
}));
context.subscriptions.push(workspace.onDidSaveTextDocument((document) => {
if (isMarkdownFile(document)) {
const uri = getPreviewUri(document.uri);
provider.update(uri);
}
}));
context.subscriptions.push(window.onDidChangeTextEditorSelection((event) => {
if (isMarkdownFile(event.textEditor.document)) {
const markdownFile = getPreviewUri(event.textEditor.document.uri);
commands.executeCommand("_workbench.htmlPreview.postMessage",
markdownFile,
{
line: event.selections[0].active.line,
});
}
}));
}
export function deactivate() {
server.stopMarkdocsServer();
}
function getPreviewUri(uri: Uri) {
if (uri.scheme === DocumentContentProvider.scheme) {
return uri;
}
return uri.with({
path: uri.fsPath + ".rendered",
query: uri.toString(),
scheme: DocumentContentProvider.scheme,
});
}
function preview(uri: Uri, viewColumn: number, provider: DocumentContentProvider) {
if (window.activeTextEditor) {
uri = uri || window.activeTextEditor.document.uri;
}
if (!uri) {
return;
}
const previewUri = getPreviewUri(uri);
provider.update(previewUri);
return commands.executeCommand("vscode.previewHtml", previewUri, viewColumn, `view ${path.basename(uri.fsPath)}`);
}
function click(uri: string, line: number) {
const sourceUri = Uri.parse(decodeURIComponent(uri));
return workspace.openTextDocument(sourceUri)
.then((document) => window.showTextDocument(document))
.then((editor) =>
commands.executeCommand("revealLine", { lineNumber: Math.floor(line), at: "center" })
.then(() => editor))
.then((editor) => {
if (editor) {
editor.selection = new Selection(
new Position(Math.floor(line), 0),
new Position(Math.floor(line), 0));
}
});
}
function reveal(uri: string, line: number) {
const sourceUri = Uri.parse(decodeURIComponent(uri));
window.visibleTextEditors
.filter((editor) => isMarkdownFile(editor.document) && editor.document.uri.toString() === sourceUri.toString())
.forEach((editor) => {
const sourceLine = Math.floor(line);
const fraction = line - sourceLine;
const text = editor.document.lineAt(sourceLine).text;
const start = Math.floor(fraction * text.length);
editor.revealRange(
new Range(sourceLine, start, sourceLine + 1, 0),
TextEditorRevealType.AtTop);
});
}