-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.ts
167 lines (157 loc) · 5.26 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from "vscode";
import { count, IWordCountResult } from "@homegrown/word-counter";
import getMarkdownContent from "./markdown";
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log(
'Congratulations, your extension "markdown-word-count" is now active!'
);
const updater = new WordCountUIUpdater();
context.subscriptions.push(updater);
updater.update();
}
type WordCountResultKeys = keyof IWordCountResult;
class WordCountUIUpdater {
private counts = [
"words" as const,
"lines" as const,
"characters" as const,
"charactersWithSpaces" as const,
];
private statusBarShownCounts: (typeof this.counts)[number][] = [];
private enableSelectionCount: boolean = false;
private statusBarItem;
private disposable: vscode.Disposable[] = [];
constructor() {
this.setConfiguration();
this.statusBarItem = vscode.window.createStatusBarItem(
vscode.StatusBarAlignment.Left
);
vscode.window.onDidChangeTextEditorSelection(
this.update,
this,
this.disposable
);
vscode.window.onDidChangeActiveTextEditor(
this.update,
this,
this.disposable
);
vscode.workspace.onDidChangeConfiguration(
this.onConfigurationChange,
this,
this.disposable
);
}
onConfigurationChange(e: vscode.ConfigurationChangeEvent) {
if (
e.affectsConfiguration("markdown-word-count.statusBarCounts") ||
e.affectsConfiguration("markdown-word-count.selectionCount")
) {
this.setConfiguration();
this.update();
}
}
setConfiguration() {
const configuration = vscode.workspace.getConfiguration(
"markdown-word-count"
);
const shownItemsConfig = configuration.get<{ [_: string]: boolean }>(
"statusBarCounts"
);
this.statusBarShownCounts = shownItemsConfig
? this.counts.filter((count) => shownItemsConfig[count])
: ["words"];
this.enableSelectionCount =
configuration.get<boolean>("selectionCount") ?? false;
}
update() {
const editor = vscode.window.activeTextEditor;
if (!editor) {
this.statusBarItem.hide();
return;
}
const isMarkdown = editor.document.languageId === "markdown";
const isPlaintext = editor.document.languageId === "plaintext";
if (!isMarkdown && !isPlaintext) {
this.statusBarItem.hide();
return;
}
try {
const docContent = editor.document.getText();
const {
content: markdownContent,
frontMatterEndLine,
} = getMarkdownContent(docContent);
const mainContent = isMarkdown ? markdownContent : docContent;
const selectionCount: IWordCountResult = {
words: 0,
lines: 0,
characters: 0,
charactersWithSpaces: 0,
};
const selectionContent = editor.selections
.map(({ start, end }) => {
if (end.line <= frontMatterEndLine) {
return "";
}
if (start.line <= frontMatterEndLine) {
start = new vscode.Position(frontMatterEndLine + 1, 0);
}
const text = editor.document.getText(new vscode.Range(start, end));
return text;
})
.filter((text) => !!text);
const showSelectionCount =
this.enableSelectionCount && selectionContent.length > 0;
if (showSelectionCount) {
selectionContent.forEach((text) => {
const countResult = count(text);
Object.keys(selectionCount).forEach((key) => {
selectionCount[key as WordCountResultKeys] +=
countResult[key as WordCountResultKeys] ?? 0;
});
});
}
const fullTextCount = count(mainContent);
const SEPARATION = "/";
const countText = {
words: `${
(showSelectionCount ? selectionCount.words + SEPARATION : "") +
fullTextCount.words
} Words`,
lines: `${
(showSelectionCount ? selectionCount.lines + SEPARATION : "") +
fullTextCount.lines
} Lines`,
characters: `${
(showSelectionCount ? selectionCount.characters + SEPARATION : "") +
fullTextCount.characters
} Characters`,
charactersWithSpaces: `${
(showSelectionCount
? selectionCount.charactersWithSpaces + SEPARATION
: "") + fullTextCount.charactersWithSpaces
} Characters with spaces`,
};
this.statusBarItem.text = `$(markdown) ${this.statusBarShownCounts
.map((id) => countText[id] ?? "")
.join(" | ")}`;
this.statusBarItem.tooltip = Object.values(countText)
.map((text) => text.replace(SEPARATION, ` ${SEPARATION} `))
.join("\n");
this.statusBarItem.show();
} catch (e) {
console.log(e);
}
}
dispose() {
this.statusBarItem.dispose();
vscode.Disposable.from(...this.disposable).dispose();
}
}