Skip to content

Commit

Permalink
fix: quick navigate to configuration file from activity
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenngoclongdev authored Oct 17, 2024
1 parent 827d6d0 commit 75601aa
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-llamas-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"terminal-keeper": patch
---

fix: quick navigate to configuration file from activity
54 changes: 54 additions & 0 deletions esbuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const esbuild = require('esbuild');

const production = process.argv.includes('--production');
const watch = process.argv.includes('--watch');

async function main() {
const ctx = await esbuild.context({
entryPoints: ['src/extension.ts'],
bundle: true,
format: 'cjs',
minify: production,
sourcemap: !production,
sourcesContent: false,
platform: 'node',
outfile: 'dist/extension.js',
external: ['vscode'],
logLevel: 'silent',
mainFields: ['module', 'main'],
plugins: [
/* add to the end of plugins array */
esbuildProblemMatcherPlugin
]
});
if (watch) {
await ctx.watch();
} else {
await ctx.rebuild();
await ctx.dispose();
}
}

/**
* @type {import('esbuild').Plugin}
*/
const esbuildProblemMatcherPlugin = {
name: 'esbuild-problem-matcher',
setup(build) {
build.onStart(() => {
console.log('Build started...');
});
build.onEnd((result) => {
result.errors.forEach(({ text, location }) => {
console.error(`✘ [ERROR] ${text}`);
console.error(` ${location.file}:${location.line}:${location.column}:`);
});
console.info('Build finished!');
});
}
};

main().catch((e) => {
console.error(e);
process.exit(1);
});
119 changes: 91 additions & 28 deletions package-lock.json

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

12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@
"title": "Refresh",
"icon": "$(refresh)"
},
{
"command": "terminal-keeper.navigate-activity",
"title": "Edit",
"icon": "$(search)"
},
{
"command": "terminal-keeper.collapse-all-activity",
"title": "Collapse All",
Expand Down Expand Up @@ -333,6 +338,11 @@
"command": "terminal-keeper.copy-command-activity",
"when": "viewItem == terminal-context",
"group": "terminal@3"
},
{
"command": "terminal-keeper.navigate-activity",
"when": "viewItem == terminal-context",
"group": "terminal@4"
}
],
"explorer/context": [
Expand Down Expand Up @@ -401,7 +411,7 @@
"cs": "changeset",
"test": "node ./dist/__test__/runTest.js",
"clean": "rm -rf dist node_modules web/build web/node_modules *.vsix",
"build": "esbuild ./src/extension.ts --bundle --outfile=dist/extension.js --external:vscode --format=cjs --platform=node",
"build": "node esbuild.js",
"build:webext": "webpack",
"watch": "tsc -watch -p ./",
"watch:webext": "webpack --watch"
Expand Down
54 changes: 54 additions & 0 deletions src/commands/navigateAsync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import path from 'path';
import { Selection, TextDocument, Uri, window, workspace } from 'vscode';
import { Configuration } from '../configuration/configuration';
import { TKTreeItem } from '../explorer/tree-provider';
import { constants } from '../utils/constants';
import { showErrorMessageWithDetail, showGenerateConfiguration } from '../utils/utils';

const getFileUriBySource = (source: string | undefined): Uri => {
if (source === 'settings.json') {
return Uri.file(path.join(Configuration.vscodeDirPath, source));
}
return Uri.file(Configuration.sessionFilePath);
};

const getKeywordRegex = (treeItem: TKTreeItem): RegExp => {
const { source, keyword } = treeItem;
let keywordFull = keyword || '';
if (source === 'settings.json') {
keywordFull = `terminal-keeper.${keyword}`;
}
return new RegExp(keywordFull, 'gm');
};

export const navigateAsync = async (treeItem: TKTreeItem): Promise<void> => {
try {
// Write configuration file
const isDefinedSessionFile = await Configuration.isDefinedSessionFile();
if (!isDefinedSessionFile) {
await showGenerateConfiguration();
return;
}

// Get source include keyword
const { source } = treeItem;
const sessionFileUri = getFileUriBySource(source);
const document: TextDocument = await workspace.openTextDocument(sessionFileUri);
const content = document.getText();

// Navigate to the session configuration
const regex = getKeywordRegex(treeItem);
const matches = [...content.matchAll(regex)];
let selections: Selection[] = [];
matches.forEach((match) => {
if (match.index) {
const startPosition = document.positionAt(match.index);
const endPosition = document.positionAt(match.index + match[0].length);
selections.push(new Selection(startPosition, endPosition));
}
});
await window.showTextDocument(document, { selection: selections?.[0] });
} catch (error) {
showErrorMessageWithDetail(constants.openConfigurationFailed, error);
}
};
2 changes: 1 addition & 1 deletion src/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { SessionConfiguration } from './interface';

export class Configuration {
private static workSpaceConfigurationSpace: string = 'terminal-keeper';
private static vscodeDirPath: string = '';
public static vscodeDirPath: string = '';
public static sessionFilePath: string = '';
public static userConfigKeys: string[] = [];

Expand Down
19 changes: 19 additions & 0 deletions src/explorer/tree-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ import {
} from 'vscode';
import { Configuration } from '../configuration/configuration';
import { SessionItem } from '../configuration/interface';
import { extCommands } from '../utils/constants';

export class TKTreeItem extends TreeItem {
sessionId: string | undefined;
terminalArrayIndex: number | undefined;
children: TKTreeItem[] | undefined;

// Use to navigate to json
source: 'settings.json' | 'sessions.json' | undefined;
keyword: string | undefined;

constructor(label: string, children?: TKTreeItem[]) {
super(label, children === undefined ? TreeItemCollapsibleState.None : TreeItemCollapsibleState.Collapsed);
this.children = children;
Expand Down Expand Up @@ -192,6 +197,13 @@ export class TreeProvider implements TreeDataProvider<TKTreeItem> {
.appendCodeblock(`Config Source: ${source}`);
item.contextValue = 'overview-context';
item.iconPath = new ThemeIcon(id || 'circle-filled', color);
item.source = source;
item.keyword = label;
item.command = {
title: 'Navigate to configuration',
command: extCommands.navigateActivity,
arguments: [item]
};
return item;
};

Expand Down Expand Up @@ -264,6 +276,13 @@ export class TreeProvider implements TreeDataProvider<TKTreeItem> {
item.iconPath = new ThemeIcon(icon?.id || 'terminal', color);
item.sessionId = sessionId;
item.terminalArrayIndex = terminalArrayIndex;
item.source = 'sessions.json';
item.keyword = terminalName;
item.command = {
title: 'Navigate to configuration',
command: extCommands.navigateActivity,
arguments: [item]
};
return item;
};
}
Loading

0 comments on commit 75601aa

Please sign in to comment.