Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving Generic Map by Reworking levelIndent #86

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/mapper_generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export class mapper {
public static generate(file: string, mappings: SyntaxMapping[]): string[] {

// # Parse
let item_max_length = 0;
let members = [];

try {
Expand All @@ -55,8 +54,6 @@ export class mapper {
lines = text.split(/\r?\n/g);

let line_num = 0;
let last_type = '';
let last_indent = 0;

lines.forEach(line => {

Expand All @@ -66,16 +63,16 @@ export class mapper {
if (line != '') {

let code_line = line.trimStart();
let level_indent = line.length - code_line.length;

for (let item of mappings) {

let m = line.match(item.regex);

if (m) {

let level_indent = line.length - code_line.length;
let level_hierarchy = 0
if (item.levelIndent)
level_indent = item.levelIndent;
level_hierarchy = item.levelIndent;

let match = m[0];

Expand All @@ -84,8 +81,7 @@ export class mapper {
.split('|')
.forEach(text => {
try {
// match = match.replaceAll(text, ''); // fails to treat arguments as regex :o(

// match = match.replaceAll(text, ''); // fails to treat arguments as regex :o(
match = match.replace(new RegExp(text, 'g'), '');
}
catch (error) {
Expand All @@ -97,8 +93,16 @@ export class mapper {
match += item.suffix;
if (item.prefix)
match = item.prefix + match;
// TODO: We should do the processing here instead.
// Collecting and processing may lead to
// misinterpretation of hierarch, hence
// when we go out of a function and enter
// another indentation and have a mapping,
// this mapping will be nested under functions
// even it is not inside the function
//
members.push([line_num, item.role, match, level_indent, item.icon, level_hierarchy]);

members.push([line_num, item.role, match, level_indent, item.icon]);
break;
}
}
Expand All @@ -120,6 +124,7 @@ export class mapper {
let content = item[2];
let indent = item[3];
let icon = item[4];
let hierarchy = item[5];
let extra_line = '';

if (indent == last_indent && content_type != last_type)
Expand All @@ -128,7 +133,8 @@ export class mapper {
let prefix = ' '.repeat(indent);
let lean_content = content.trimStart();

map = map + extra_line + prefix + lean_content + '|' + String(line) + '|' + icon + '\n';
map = map + extra_line + prefix + lean_content + '|' +
String(line) + '|' + icon + '|'+ String(hierarchy) + '\n';

last_indent = indent;
last_type = content_type;
Expand Down
180 changes: 147 additions & 33 deletions src/tree_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ export class SettingsTreeProvider implements vscode.TreeDataProvider<SettingsIte

let ArrCodeMapTypes = Array.from(codeMapTypes);

// "backup" settings defined here. This is done to keep the settings chosen that aren't the default
// "backup" settings defined here. This is done to keep the settings chosen that aren't the default
// available for reinstatement after a whole tree rebuild is triggered by a file save or window change.
// Sadly we need this hack rather than only causing a rebuild on changed nodes as more nodes may
// Sadly we need this hack rather than only causing a rebuild on changed nodes as more nodes may
// have been added.
if (!(Object.keys(this._settings).includes(filename))) {
this._settings[filename] = {};
Expand Down Expand Up @@ -243,6 +243,89 @@ export class FavoritesTreeProvider implements vscode.TreeDataProvider<MapItem> {
return this.Items;
}

public static process_node(
node : MapItem,
nodes : MapItem[],
map_hierarchy : MapItem[]
): void {
let level_indent = node.level_indent;
let level_hierarchy = node.level_hierarchy;

if (!(map_hierarchy.length)) {
nodes.push(node)

if (level_hierarchy)
{
node = nodes[nodes.length - 1]
map_hierarchy.push(node)
}
}
else if (map_hierarchy.length) {
// Let us get parent
let parent : MapItem = map_hierarchy[map_hierarchy.length - 1];

if (level_indent > parent.level_indent)
{
if (level_hierarchy)
{
map_hierarchy.push(node);
}

parent.addChildItem(node);
node.parent = parent;

}
else if (level_indent == parent.level_indent)
{
if (level_hierarchy)
{
if (level_hierarchy < parent.level_hierarchy)
{
map_hierarchy.pop()
FavoritesTreeProvider.process_node(
node,
nodes,
map_hierarchy
)
}
else if (level_hierarchy == parent.level_hierarchy)
{
if (parent.parent) {
parent.parent.addChildItem(node)
node.parent = parent.parent
}
else {
nodes.push(node)
}

map_hierarchy.pop()
}
else
{
// TODO Implementing outline type, that different types
// TODO will not race hierarchy (nestable will not nest
// TODO the other type on same indent level)
parent.addChildItem(node);
node.parent = parent;
}

map_hierarchy.push(node)
}
else
{
parent.addChildItem(node);
node.parent = parent;
}
}
else if (level_indent < parent.level_indent)
{
map_hierarchy.pop()
FavoritesTreeProvider.process_node(node, nodes, map_hierarchy)

}
}
}

public static parseScriptItems(items: string[], sourceFile: string, nodeTypesToKeep: string[]): MapItem[] {

let nodes = [];
Expand All @@ -251,44 +334,63 @@ export class FavoritesTreeProvider implements vscode.TreeDataProvider<MapItem> {
// https://github.com/patrys/vscode-code-outline/issues/24: Is it possible to disable expand/collapse on click
// Until above items are fixed need to go with the plain text.
let plainTextMode = vscode.workspace.getConfiguration("codemap").get('textMode', defaults.get('textMode'));
let max_nesting_level = vscode.workspace.getConfiguration("codemap").get('maxNestingLevel', defaults.get('maxNestingLevel'));
let max_hierarchy_level = vscode.workspace.getConfiguration("codemap").get('maxNestingLevel', defaults.get('maxNestingLevel'));

// default is empty (non-white space) character U+00A0; to avoid trimming by treeview renderer
let levelUnitChar = vscode.workspace.getConfiguration("codemap").get('textModeLevelPrefix', defaults.get('textModeLevelPrefix'));

let levelUnit = null;
// Intelligently taking tab size (in terms of number of spaces) from active text editor
let levelUnit : number;
let levelUnit_t = vscode.window.activeTextEditor.options.tabSize;

// Language
let editor_language = vscode.window.activeTextEditor.document.languageId;

if (typeof levelUnit_t === "string")
{
// TODO Set indentation according to language or
// TODO scrape it from document
editor_language;

levelUnit = 4;
}
else
{
levelUnit = levelUnit_t
}
let map: { [index: number]: MapItem; } = {};
let map_hierarchy = [];

items.forEach(item => {

if (item != '') {
let source_file = sourceFile;
let tokens = item.split('|');

let lineNumber = 0;
let icon = 'document';

let title: string = item;

let nesting_level = item.length - item.trimStart().length;

if (nesting_level != 0) {
if (!levelUnit)
levelUnit = nesting_level;
nesting_level = nesting_level / levelUnit;
}

let title: string;
let level_indent = item.length - item.trimStart().length;
let level_hierarchy:number = 0;
if (tokens.length > 1) {
try {
title = tokens[0];
lineNumber = Number(tokens[1]) - 1;
icon = tokens[2];
level_hierarchy = Number(tokens[3]);
} catch (error) {
}
}
else
source_file = null;

if (nesting_level > max_nesting_level)
// if (level_indent != 0) {
// if (!levelUnit)
// levelUnit = level_indent;
// level_indent = level_indent / levelUnit;
// }

if (level_hierarchy > max_hierarchy_level)
return;

// the normal spaces are collapsed by the tree item renderer
Expand All @@ -308,7 +410,8 @@ export class FavoritesTreeProvider implements vscode.TreeDataProvider<MapItem> {
let node = new MapItem(
title,
textModeExpanded ? vscode.TreeItemCollapsibleState.Expanded : vscode.TreeItemCollapsibleState.Collapsed,
nesting_level,
level_indent,
level_hierarchy,
{
command: on_click_command,
title: '',
Expand All @@ -322,23 +425,30 @@ export class FavoritesTreeProvider implements vscode.TreeDataProvider<MapItem> {
if (nodeTypesToKeep.includes(icon)) {
if (plainTextMode) {
node.collapsibleState = vscode.TreeItemCollapsibleState.None;
node.label = non_whitespace_empty_char.repeat(nesting_level) + title;
node.label = non_whitespace_empty_char.repeat(Math.floor(level_indent / levelUnit)) + title;
nodes.push(node);
}
else {
if (nesting_level == 0) {
nodes.push(node);
}
else {
let parent = map[node.nesting_level - 1];
if (!parent) {
for (let key in map) {
parent = map[key];
}
}
parent.addChildItem(node);
node.parent = parent;
}

FavoritesTreeProvider.process_node(
node,
nodes,
map_hierarchy
)

// if (level_indent == 0) {
// nodes.push(node);
// }
// else {
// let parent = map[node.nesting_level - 1];
// if (!parent) {
// for (let key in map) {
// parent = map[key];
// }
// }
// parent.addChildItem(node);
// node.parent = parent;
// }
}
}

Expand All @@ -358,7 +468,7 @@ export class FavoritesTreeProvider implements vscode.TreeDataProvider<MapItem> {
};
}

map[node.nesting_level] = node;
map[node.level_indent] = node;
}
});

Expand All @@ -383,12 +493,16 @@ function getDefaultSortDirection() {
return SortDirection[dir];
}

// TODO Implementing outline type, that different types will not
// TODO race hierarchy (nestable will not nest the other type on same
// TODO indent level)
export class MapItem extends vscode.TreeItem {

constructor(
public readonly title: string,
public readonly state: vscode.TreeItemCollapsibleState,
public readonly nesting_level: number,
public readonly level_indent: number,
public readonly level_hierarchy: number,
public readonly command?: vscode.Command,
public readonly context?: string,
public readonly lineNumber?: number) {
Expand Down