Skip to content

Commit

Permalink
refactor: optimize performance
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinhuish committed Dec 11, 2024
1 parent f27cbb3 commit b52ab67
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"publisher": "edwinhuish",
"name": "better-comments-next",
"displayName": "Better Comments Next",
"version": "3.3.12",
"version": "3.3.13",
"description": "Improve your code commenting by annotating with alert, informational, TODOs, and more!",
"author": {
"name": "Edwin Xu"
Expand Down
20 changes: 20 additions & 0 deletions src/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export interface ConfigurationFlatten extends Configuration {
let config: (Configuration & WorkspaceConfiguration) | undefined;
let configFlatten: ConfigurationFlatten | undefined;
let tagDecorationTypes: Map<string, vscode.TextEditorDecorationType> | undefined;
let multilineTagsEscaped: string[] | undefined;
let lineTagsEscaped: string[] | undefined;

export function refresh() {
// if already set tagDecorationTypes, clear decoration for visible editors
Expand All @@ -72,6 +74,8 @@ export function refresh() {
config = undefined;
configFlatten = undefined;
tagDecorationTypes = undefined;
multilineTagsEscaped = undefined;
lineTagsEscaped = undefined;
}

/**
Expand Down Expand Up @@ -182,3 +186,19 @@ function parseDecorationRenderOption(tag: TagFlatten) {

return options;
}

export function getMultilineTagsEscaped() {
if (!multilineTagsEscaped) {
multilineTagsEscaped = getConfigurationFlatten().tags.filter(t => t.multiline).map(tag => tag.tagEscaped);
}

return multilineTagsEscaped;
}

export function getLineTagsEscaped() {
if (!lineTagsEscaped) {
lineTagsEscaped = getConfigurationFlatten().tags.filter(t => !t.multiline).map(tag => tag.tagEscaped);
}

return lineTagsEscaped;
}
18 changes: 6 additions & 12 deletions src/handler/langs/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ function logMissingLineComments(languageId: string) {
export async function pickLineCommentDecorationOptions({ editor, processed = [] }: PickDecorationOptionsParams) {
const decorationOptions = new Map<string, vscode.DecorationOptions[]>();

const configs = configuration.getConfigurationFlatten();

const comments = await definition.getAvailableComments(editor.document.languageId);

if (!comments.lineComments || !comments.lineComments.length) {
Expand All @@ -95,8 +93,8 @@ export async function pickLineCommentDecorationOptions({ editor, processed = []

const blockExp = new RegExp(`(${escapedMarks}).*?(?:\\n[ \\t]*\\1.*?)*(\\n|$)`, 'g');

const multilineTags = configs.tags.filter(t => t.multiline).map(tag => tag.tagEscaped);
const lineTags = configs.tags.filter(t => !t.multiline).map(tag => tag.tagEscaped);
const multilineTags = configuration.getMultilineTagsEscaped();
const lineTags = configuration.getLineTagsEscaped();

const text = editor.document.getText();

Expand Down Expand Up @@ -203,12 +201,10 @@ export async function pickBlockCommentDecorationOptions({ editor, processed = []
return decorationOptions;
}

const configs = configuration.getConfigurationFlatten();

const multilineTags = configs.tags.filter(t => t.multiline).map(tag => tag.tagEscaped);
const multilineTags = configuration.getMultilineTagsEscaped();
const m1Exp = new RegExp(`([ \\t])(${multilineTags.join('|')})([\\s\\S]*?)(\\n\\s*\\n|$)`, 'gi');

const lineTags = configs.tags.filter(t => !t.multiline).map(tag => tag.tagEscaped);
const lineTags = configuration.getLineTagsEscaped();
const lineExp = new RegExp(`(^|[ \\t])(${lineTags.join('|')})([^\\n]*?)(\\n|$)`, 'gi');

for (const marks of comments.blockComments) {
Expand Down Expand Up @@ -314,18 +310,16 @@ export async function pickDocCommentDecorationOptions({ editor, processed = [] }
const end = escapeRegexString(marks[1]);
const pre = escapeRegexString(prefix);

const configs = configuration.getConfigurationFlatten();

const blockExp = new RegExp(`(^|\\s)(${start})(([ \\t]+|[ \\t]*\\n)[\\s\\S]*?)(${end})`, 'g');

const multilineTags = configs.tags.filter(t => t.multiline).map(tag => tag.tagEscaped);
const multilineTags = configuration.getMultilineTagsEscaped();
const m1Exp = new RegExp(
`([ \\t]*(${pre}?)[ \\t])((${multilineTags.join('|')})([\\s\\S]*?))(\\n(\\s*${pre}?\\s*)\\n|$)`,
'gi',
);
const m2Exp = new RegExp(`(^|[ \\t]*(${pre}))([^\\n]*?)(\\n|$)`, 'gi');

const lineTags = configs.tags.filter(t => !t.multiline).map(tag => tag.tagEscaped);
const lineTags = configuration.getLineTagsEscaped();
const lineExp = new RegExp(`(^|[ \\t]*(${pre})[ \\t])(${lineTags.join('|')})([^\\n]*?)(\\n|$)`, 'gi');

const text = editor.document.getText();
Expand Down
4 changes: 2 additions & 2 deletions src/handler/langs/plaintext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ async function pickDecorationOptions({ editor }: { editor: vscode.TextEditor })
return decorationOptions;
}

const multilineTags = configs.tags.filter(t => t.multiline).map(tag => tag.tagEscaped);
const lineTags = configs.tags.filter(t => !t.multiline).map(tag => tag.tagEscaped);
const multilineTags = configuration.getMultilineTagsEscaped();
const lineTags = configuration.getLineTagsEscaped();

const lineProcessed: [number, number][] = [];

Expand Down

0 comments on commit b52ab67

Please sign in to comment.