Skip to content

Commit

Permalink
add unique index name
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-oser committed Nov 1, 2021
1 parent b19b423 commit 2ef3960
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,8 @@
"eslint": "^8.1.0",
"typescript": "^4.4.4",
"vsce": "^1.95.1"
},
"dependencies": {
"nanoid": "^3.1.30"
}
}
15 changes: 8 additions & 7 deletions src/foldingProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from "vscode";
import treeStringToJson from "./tree";
import { INDEX_NAME, treeStringToJson } from "./tree";

class FoldingProvider implements vscode.FoldingRangeProvider {
onDidChangeFoldingRanges?: vscode.Event<void> | undefined;
Expand All @@ -10,11 +10,12 @@ class FoldingProvider implements vscode.FoldingRangeProvider {
): vscode.ProviderResult<vscode.FoldingRange[]> {
const getLastNode = (branch: Record<string, any>) => {
const indexes: Array<number> = [];
// Get the last line number that is a child of the given branch
const getIndexes = (branch: Record<string, any>) => {
if (Object.values(branch).length === 1) {
indexes.push(branch.__index);
indexes.push(branch[INDEX_NAME]);
} else {
Object.values(branch).forEach(child => {
Object.values(branch).forEach((child) => {
getIndexes(child);
});
}
Expand All @@ -26,17 +27,17 @@ class FoldingProvider implements vscode.FoldingRangeProvider {
const ranges = new Array<vscode.FoldingRange>();
const getRanges = (branch: Record<string, any>) => {
const children = Object.values(branch);
if (children.length == 1) return;
const startIndex = branch.__index;
if (children.length == 1) return;
const startIndex = branch[INDEX_NAME];
const endIndex = getLastNode(branch);
ranges.push(new vscode.FoldingRange(startIndex, endIndex, vscode.FoldingRangeKind.Region));
children.forEach((child) => {
getRanges(child);
});
};

const tree = treeStringToJson(document.getText());
Object.values(tree).forEach(branch => {
Object.values(tree).forEach((branch) => {
getRanges(branch);
});

Expand Down
14 changes: 8 additions & 6 deletions src/tree.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { nanoid } from "nanoid";

// Use a unique index so that it won't conflict with any file names
export const INDEX_NAME = nanoid();

const getNumberOfTabs = (line: string) => {
return (line.match(/\t/g) || []).length;
};

const treeStringToJson = (tree: string) => {
export const treeStringToJson = (tree: string) => {
const elements = new Set();
let prevLine = "";
const path: Array<string> = [];
Expand Down Expand Up @@ -38,12 +43,9 @@ const treeStringToJson = (tree: string) => {
(branch: any, filename: string) => branch[filename],
elements
);

current[filename] = { __index: index };
current[filename] = { [INDEX_NAME]: index };
prevLine = line;
path.push(filename);
});
return elements;
};

export default treeStringToJson;
};

0 comments on commit 2ef3960

Please sign in to comment.