Skip to content

Commit

Permalink
Release v1.21.0
Browse files Browse the repository at this point in the history
- Added support for GO syntax (*.go files).
- Added workflow for editing the mapper of the active opened document.
- Added workflow for creating a new mapper for the active opened document.
- New commands: `codemap.edit_mapper` and `codemap.create_mapper`
  • Loading branch information
oleg-shilo committed Feb 4, 2024
1 parent 506e23b commit dc2f421
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 188 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Change Log

## Next
## 1.21.0 (5 February 2024)

- Added support for GO syntax (*.go files).
- Added workflow for editing the mapper of the active opened document.
- Added workflow for creating a new mapper for the active opened document.
- New commands: `codemap.edit_mapper` and `codemap.create_mapper`

## 1.20.5 (30 January 2024)

Expand Down
30 changes: 29 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "codemap",
"displayName": "CodeMap",
"description": "Interactive code map for quick visualization and navigation within code DOM objects (e.g. classes, members).",
"version": "1.20.5",
"version": "1.21.0",
"license": "MIT",
"publisher": "oleg-shilo",
"engines": {
Expand Down Expand Up @@ -187,6 +187,26 @@
],
"description": "Regex-based definition of the mapping rules for INI file syntax."
},
"codemap.go": {
"type": "array",
"default": [
{
"pattern": "func (.*?)\\)(.*?){",
"clear": "func|(|{",
"suffix": "",
"role": "function",
"icon": "function"
},
{
"pattern": "type (.*?) struct(.*?){",
"clear": "type|struct|(|{",
"suffix": "",
"role": "class",
"icon": "class"
}
],
"description": "Regex-based definition of the mapping rules for GO file syntax."
},
"codemap.r": {
"type": "array",
"default": [
Expand Down Expand Up @@ -396,6 +416,14 @@
"dark": "resources/dark/arrow-both.svg"
}
},
{
"command": "codemap.create_mapper",
"title": "CodeMap: Create a mapper for the currently opened file",
"icon": {
"light": "resources/light/arrow-both.svg",
"dark": "resources/dark/arrow-both.svg"
}
},
{
"command": "codemap.mappers",
"title": "CodeMap: Show mappers configuration"
Expand Down
166 changes: 156 additions & 10 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as cs from "./mapper_cs";
import * as generic from "./mapper_generic";
import * as md from "./mapper_md";
import { SyntaxMapping } from "./mapper_generic";
import { Utils, config_defaults, } from "./utils";
import { Utils, config_defaults } from "./utils";

const defaults = new config_defaults();
let treeViewProvider1: FavoritesTreeProvider;
Expand Down Expand Up @@ -88,10 +88,7 @@ function get_map_items(): MapInfo {
}
}

if (
document.toLowerCase().endsWith(".ts") ||
document.toLowerCase().endsWith(".js")
) {
if (document.toLowerCase().endsWith(".ts") || document.toLowerCase().endsWith(".js")) {
// dedicated built-in mapper
return { sourceFile: document, items: ts.mapper.generate(document) };
}
Expand Down Expand Up @@ -246,22 +243,44 @@ function get_required_mapper_type() {
return null;
}

function edit() {
function edit_mapper() {

try {

let value_name = get_required_mapper_type();
if (value_name) {

let config = vscode.workspace.getConfiguration("codemap");

let mapper = config.get("overloaded." + value_name, null);

if (mapper == null)
let settingsFile = path.join(cs.user_dir(), "..", "settings.json");
let existingMapperIndex = -1;

if (mapper == null) {
mapper = config.get(value_name, defaults.get(value_name));

let lines = fs.readFileSync(settingsFile, 'utf8').split(/\r?\n/g);

let rgx = new RegExp('\"codemap\.' + value_name.toLowerCase() + '\":', "g");
for (let i = 0; i < lines.length; i++) {
if (existingMapperIndex == -1 && lines[i].match(rgx)) {
existingMapperIndex = i;
break;
}
}
}

mapper = get_actual_mapper(mapper);

if (!mapper) {
// dedicated built-in mappers
if (value_name == "ts" || value_name == "js") {
mapper = "mapper_ts.js";
} else if (value_name == "cs") {
mapper = "mapper_cs.js";
}
}

if (mapper) {

if (typeof mapper == "string") { // custom dedicated mapper (path string)
Expand All @@ -277,7 +296,17 @@ function edit() {
commands.executeCommand('vscode.open', Uri.file(file));
}
else { // Generic mapper (an object)
vscode.commands.executeCommand('workbench.action.openSettings', 'codemap.' + value_name);
if (existingMapperIndex != null) { // the mapper is in the user settings file
commands.executeCommand('vscode.open', Uri.file(settingsFile))
.then(() => {
let editor = vscode.window.activeTextEditor;
let range = editor.document.lineAt(existingMapperIndex).range;
editor.selection = new vscode.Selection(range.start, range.end);
editor.revealRange(range);
});
} else {
vscode.commands.executeCommand('workbench.action.openSettings', 'codemap.' + value_name);
}
}
return;
}
Expand All @@ -290,6 +319,122 @@ function edit() {
}
}

function create_mapper() {

var mapper_type = get_required_mapper_type();
var dedicated = 'Dedicated mapper (JS file)';
var generic = 'Generic mapper (regular expression in the settings file)';

vscode.window
.showQuickPick([dedicated, generic])
.then(selectedItem => {

if (selectedItem == dedicated) {
var doc = path.join(cs.user_dir(), `mapper_${mapper_type}.js`);
if (fs.existsSync(doc)) {
vscode.window.showErrorMessage(
'The current document mapper already exists. Opening it instead of creating a new one.');
}
else {
var code = `"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
class mapper {
static read_all_lines(file) {
let text = fs.readFileSync(file, 'utf8');
return text.split(/\\r?\\n/g);
}
static generate(file) {
let members = [];
let line_num = 0;
try {
mapper
.read_all_lines(file)
.forEach(line => {
line_num++;
if(line_num < 3) // demo first 3 lines only
members.push(\`item|\${line_num}|level3\`);
});
}
catch (error) {
}
return members;
}
}
exports.mapper = mapper;`;

fs.writeFileSync(doc, code, { encoding: 'utf8' });
}
commands.executeCommand('vscode.open', Uri.file(doc));
}
else if (selectedItem == generic) {

let settingsFile = path.join(cs.user_dir(), "..", "settings.json");
let lines = fs.readFileSync(settingsFile, 'utf8').split(/\r?\n/g);

let rgx = new RegExp('\"codemap\.' + mapper_type.toLowerCase() + '\":', "g");
let firstMapperIndex = -1;
let existingMapperIndex = -1;
for (let i = 0; i < lines.length; i++) {
if (firstMapperIndex == -1 && lines[i].match(/(\s*)\"\w+\.\w+\"\:(\s*)\[/g))
firstMapperIndex = i;
if (existingMapperIndex == -1 && lines[i].match(rgx)) {
existingMapperIndex = i;
break;
}
}

let selectedLine = 1;
if (existingMapperIndex == -1) {
selectedLine = firstMapperIndex;
} else {
selectedLine = existingMapperIndex;
}

commands.executeCommand('vscode.open', Uri.file(settingsFile))
.then(() => {

let editor = vscode.window.activeTextEditor;
let range = editor.document.lineAt(selectedLine).range;

if (existingMapperIndex == -1) {
range = editor.document.lineAt(selectedLine - 1).range; // one line above
editor.selection = new vscode.Selection(range.start, range.start);

const editRange = editor.document.lineAt(editor.selection.end.line).range.end;

editor.edit(editBuilder => {
if (editor !== undefined) {
editBuilder.insert(editRange, `
"codemap.${mapper_type.toLowerCase()}": [
{
"pattern": "function (.*?)[(|:{]",
"clear": "({",
"suffix": "()",
"role": "function",
"icon": "function"
}
],`);
}

});
range = editor.document.lineAt(selectedLine).range;
editor.selection = new vscode.Selection(range.start, range.end);
}
else {
editor.selection = new vscode.Selection(range.start, range.end);
vscode.window.showErrorMessage(
'The current document mapper already exists. Opening it instead of creating a new one.');
}

editor.revealRange(range);
});

}
});
}


function quick_pick() {

let info = get_map_items();
Expand Down Expand Up @@ -352,7 +497,8 @@ export function activate(context: vscode.ExtensionContext) {
let treeView1 = vscode.window.createTreeView("codemap-own-view", { treeDataProvider: treeViewProvider1, showCollapseAll: true });
let treeView2 = vscode.window.createTreeView("codemap-explorer-view", { treeDataProvider: treeViewProvider2, showCollapseAll: true });

vscode.commands.registerCommand("codemap.edit_mapper", edit);
vscode.commands.registerCommand("codemap.edit_mapper", edit_mapper);
vscode.commands.registerCommand("codemap.create_mapper", create_mapper);

vscode.commands.registerCommand("codemap.reveal", () => reveal_current_line_in_tree(treeView1, treeView2));
vscode.commands.registerCommand("codemap.quick_pick", quick_pick);
Expand Down
Loading

0 comments on commit dc2f421

Please sign in to comment.