Skip to content

Commit cf1a7e6

Browse files
committed
Cache the MultiTablePrettyfier to make subsequent document, selection and command formatting faster
1 parent 3ae0fa4 commit cf1a7e6

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

src/extension/extension.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
'use strict';
22
import * as vscode from 'vscode';
3-
import { getSupportLanguageIds, getDocumentRangePrettyfier, getDocumentPrettyfier, getDocumentPrettyfierCommand } from './prettyfierFactory';
3+
import { getSupportLanguageIds, getDocumentRangePrettyfier, getDocumentPrettyfier, getDocumentPrettyfierCommand, invalidateCache } from './prettyfierFactory';
44

55
// This method is called when the extension is activated.
66
// The extension is activated the very first time the command is executed.
77
export function activate(context: vscode.ExtensionContext): void {
88

9+
// Invalidate cache when configuration changes
10+
context.subscriptions.push(
11+
vscode.workspace.onDidChangeConfiguration(event => {
12+
if (event.affectsConfiguration("markdownTablePrettify")) {
13+
invalidateCache();
14+
}
15+
})
16+
);
17+
918
const supportedLanguageIds = getSupportLanguageIds();
1019
for (let language of supportedLanguageIds) {
1120
context.subscriptions.push(

src/extension/prettyfierFactory.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ import { SingleTablePrettyfier } from '../prettyfiers/singleTablePrettyfier';
2424
import { TableStringWriter } from "../writers/tableStringWriter";
2525
import { ValuePaddingProvider } from '../writers/valuePaddingProvider';
2626

27+
let cachedMultiTablePrettyfier: MultiTablePrettyfier | null = null;
28+
29+
export function invalidateCache() {
30+
cachedMultiTablePrettyfier = null;
31+
}
32+
2733
export function getSupportLanguageIds() {
2834
return [ "markdown", ...getConfigurationValue<Array<string>>("extendedLanguages", []) ];
2935
}
@@ -41,15 +47,21 @@ export function getDocumentPrettyfierCommand(): TableDocumentPrettyfierCommand {
4147
}
4248

4349
function getMultiTablePrettyfier(): MultiTablePrettyfier {
50+
if (cachedMultiTablePrettyfier) {
51+
return cachedMultiTablePrettyfier;
52+
}
53+
4454
const loggers = getLoggers();
4555
const sizeLimitCheker = getSizeLimitChecker(loggers);
4656
const columnPadding: number = getConfigurationValue<number>("columnPadding", 0);
4757

48-
return new MultiTablePrettyfier(
58+
cachedMultiTablePrettyfier = new MultiTablePrettyfier(
4959
new TableFinder(new TableValidator(new SelectionInterpreter(true))),
5060
getSingleTablePrettyfier(loggers, sizeLimitCheker, columnPadding),
5161
sizeLimitCheker
5262
);
63+
64+
return cachedMultiTablePrettyfier;
5365
}
5466

5567
function getSingleTablePrettyfier(loggers: ILogger[], sizeLimitCheker: ConfigSizeLimitChecker, columnPadding: number): SingleTablePrettyfier {

0 commit comments

Comments
 (0)