Skip to content

Commit

Permalink
fix: massively increase regexp efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
1nVitr0 committed Aug 5, 2024
1 parent fe888b0 commit 26865d2
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"titleBar.activeBackground": "#2c665a",
"titleBar.activeForeground": "#e7e7e7",
"titleBar.inactiveBackground": "#2c665a99",
"titleBar.inactiveForeground": "#e7e7e799"
"titleBar.inactiveForeground": "#e7e7e799",
"commandCenter.border": "#e7e7e799"
},
"peacock.color": "#2c665a"
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@
"format": "regex",
"description": "Regex to match block headers that should be sorted last. `^` and `$` will be expanded to allow comments",
"scope": "language-overridable",
"default": "^(\\s*(when|case)\\s*('([^']|(?<=\\\\)')*'|\"([^\"]|(?<=\\\\)\")*\"|`([^`]|(?<=\\\\)`)*`|[A-Za-z_+\\-*/%<>d.,s]*)*\\s*(.*:)?\\n?\\r?)*\\s*default|else(?!\\s?if)\\s*:?$"
"default": "^(\\s*(when|case)\\s*('([^']|(?<=\\\\)')*'|\"([^\"]|(?<=\\\\)\")*\"|`([^`]|(?<=\\\\)`)*`|[A-Za-z_+\\-*\\/%<>\\d.,\\s]*)*\\s*(.*:)?\\n?\\r?)*\\s*default|else(?!\\s?if)\\s*:?$"
},
"blocksort.multiBlockHeaderRegex": {
"type": "string",
Expand All @@ -468,7 +468,7 @@
"format": "regex",
"description": "Regex for incomplete blocks. `^` and `$` will be expanded to allow comments",
"scope": "language-overridable",
"default": "(if|when|else|case|for|foreach|else|elsif|while|def|then|default)\\s*('([^']|(?<=\\\\)')*'|\"([^\"]|(?<=\\\\)\")*\"|`([^`]|(?<=\\\\)`)*`|[A-Za-z_+\\-*/%<>d.,s]*)*\\s*(.*:)?$"
"default": "(if|when|else|case|for|foreach|else|elsif|while|def|then|default)\\s*([A-Za-z_+\\-*\\/%<>\\d.,\\s]*|.*:)?$"
},
"blocksort.keepAppendedNewlines": {
"type": "boolean",
Expand Down
25 changes: 18 additions & 7 deletions src/providers/BlockSortProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,16 +354,27 @@ export default class BlockSortProvider implements Disposable {
const lines = text.split(/\r?\n/);

const lineMetas = lines.map((line, j) => {
const noComments = this.stringProcessor.stripComments(line).trim();
const noStringsOrComments = this.stringProcessor.stripStrings(noComments).trim();

const hasContent = !!noComments;
const indent = this.stringProcessor.getIndent(line, tabSize);
const folding = this.stringProcessor.getFolding(noStringsOrComments, this.document, undefined, true);
const valid = this.stringProcessor.isValidLine(line, this.document, folding);
const ignoreIndent = this.stringProcessor.isIndentIgnoreLine(line, this.document);
const complete = this.stringProcessor.isCompleteBlock(noComments, this.document);
const incomplete = this.stringProcessor.isIncompleteBlock(noComments);

return {
line: start.line + j,
indent: this.stringProcessor.getIndent(line, tabSize),
valid: this.stringProcessor.isValidLine(line, this.document),
folding: this.stringProcessor.getFolding(line, this.document),
ignoreIndent: this.stringProcessor.isIndentIgnoreLine(line, this.document),
hasContent: !!this.stringProcessor.stripComments(line).trim(),
complete: this.stringProcessor.isCompleteBlock(line, this.document),
incomplete: this.stringProcessor.isIncompleteBlock(line),
text: withText ? line : null,
folding,
indent,
valid,
ignoreIndent,
hasContent,
complete,
incomplete,
};
});

Expand Down
2 changes: 1 addition & 1 deletion src/providers/ConfigurationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export default class ConfigurationProvider {
public static getIncompleteBlockRegex(document?: TextDocument): string {
return (
ConfigurationProvider.getConfiguration(document).incompleteBlockRegex ||
"(if|when|else|case|for|foreach|else|elsif|while|def|then|default)\\s*('([^']|(?<=\\\\)')*'|\"([^\"]|(?<=\\\\)\")*\"|`([^`]|(?<=\\\\)`)*`|[A-Za-z_+\\-*/%<>d.,s]*)*\\s*(.*:)?$"
"(if|when|else|case|for|foreach|else|elsif|while|def|then|default)\\s*([A-Za-z_+\\-*\\/%<>\\d.,\\s]*|.*:)?$"
);
}

Expand Down
16 changes: 10 additions & 6 deletions src/providers/StringProcessingProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ export default class StringProcessingProvider {
return { min, max };
}

public getFolding(text: string, document: TextDocument, initial: Folding = initialFolding(document)): Folding {
public getFolding(
text: string,
document: TextDocument,
initial: Folding = initialFolding(document),
isSanitized?: boolean
): Folding {
const foldingMarkers = ConfigurationProvider.getFoldingMarkers(document);
const result: Folding = { ...initial };

Expand All @@ -89,7 +94,7 @@ export default class StringProcessingProvider {
markerKeys.push("{}"); // ensure '{}' is last to make abort on curly braces possible

for (const line of lines) {
const sanitized = this.stripStrings(this.stripComments(line)).trim();
const sanitized = isSanitized ? line : this.stripStrings(this.stripComments(line)).trim();
for (const [key, marker] of Object.entries(foldingMarkers)) {
if (!marker) continue;

Expand Down Expand Up @@ -147,8 +152,7 @@ export default class StringProcessingProvider {

public isCompleteBlock(block: string, document: TextDocument): boolean {
const completeBlockMarkers = ConfigurationProvider.getCompleteBlockMarkers(document);
const comment = commentRegex[this.document.languageId || "default"] || commentRegex.default;
const completeBlockRegex = `(?:${completeBlockMarkers.join("|")})(?:,|;)?(?:${comment}|\\s*)*(?:,|;)?$`;
const completeBlockRegex = `(?:${completeBlockMarkers.join("|")})\\s*(?:,|;)?$`;
return new RegExp(completeBlockRegex, "g").test(block);
}

Expand All @@ -175,11 +179,11 @@ export default class StringProcessingProvider {
return new RegExp(lastRegex, "g").test(block);
}

public isValidLine(line: string | LineMeta, document: TextDocument): boolean {
public isValidLine(line: string | LineMeta, document: TextDocument, folding?: Folding): boolean {
if (typeof line !== "string") return line.valid;

const comment = commentRegex[this.document.languageId || "default"] || commentRegex.default;
const hasFolding = this.hasFolding(this.getFolding(line, document));
const hasFolding = this.hasFolding(folding ?? this.getFolding(line, document));
return (
!/^\s*$/.test(line) &&
!/^\s*@/.test(line) &&
Expand Down
5 changes: 5 additions & 0 deletions src/test/fixtures/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ export const sortTests: CompareTest[] = [
compareFile: "block.go.expect",
ranges: [new Range(1, 0, 3, 10), new Range(7, 0, 9, 10), new Range(13, 0, 15, 10), new Range(18, 0, 27, 1), new Range(29, 0, 38, 1)],
},
{
file: "block.markdown.fixture",
compareFile: "block.markdown.expect",
ranges: [new Range(0, 0, 15, 21)],
},
];
3 changes: 3 additions & 0 deletions src/test/suite/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as path from 'path';
import * as Mocha from 'mocha';
import * as glob from 'glob';
import { commands } from 'vscode';

export function run(): Promise<void> {
// Create the mocha test
Expand All @@ -27,6 +28,8 @@ export function run(): Promise<void> {
} catch (err) {
console.error(err);
e(err);
} finally {
commands.executeCommand('workbench.action.closeAllEditors');
}
});
});
Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/block.markdown.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
- [ ] item with link [abc](www.google.com)
- [ ] item with loing link [abc](www.google.com/this/can/go/on/for-a-while/lets/seehowlongitcango/words/words/words/words/words/cat/dog)
- [ ] unchecked item 0
- [ ] unchecked item 1
- [ ] unchecked item 2
- [ ] unchecked item 3
- [ ] unchecked item 4
- [ ] unchecked item 5
- [ ] unchecked item 6
- [x] checked item 0
- [x] checked item 1
- [x] checked item 2
- [x] checked item 3
- [x] checked item 4
- [x] checked item 5
- [x] checked item 6
16 changes: 16 additions & 0 deletions test/fixtures/block.markdown.fixture
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
- [x] checked item 3
- [ ] unchecked item 5
- [ ] unchecked item 4
- [ ] item with loing link [abc](www.google.com/this/can/go/on/for-a-while/lets/seehowlongitcango/words/words/words/words/words/cat/dog)
- [x] checked item 0
- [ ] item with link [abc](www.google.com)
- [ ] unchecked item 0
- [x] checked item 4
- [ ] unchecked item 2
- [x] checked item 5
- [ ] unchecked item 6
- [ ] unchecked item 3
- [x] checked item 2
- [ ] unchecked item 1
- [x] checked item 6
- [x] checked item 1

0 comments on commit 26865d2

Please sign in to comment.