Skip to content

Commit

Permalink
Makes helpView dynamic by parsing user keybindings (#24) (#37)
Browse files Browse the repository at this point in the history
* makes helpView dynamic by parsing user keybindings (#24)

* makes helpview use 3 rows to avoid wrapping

* adds '-' key to grammar
  • Loading branch information
kahole committed Jun 3, 2020
1 parent b9f1524 commit 3660bfa
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 45 deletions.
18 changes: 16 additions & 2 deletions src/commands/helpCommands.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import { window, workspace } from 'vscode';
import MagitUtils from '../utils/magitUtils';
import { HelpView } from '../views/helpView';
import { views } from '../extension';
import { views, logPath } from '../extension';
import { MagitRepository } from '../models/magitRepository';
import * as path from 'path';

export async function magitHelp(repository: MagitRepository) {

let keybindingsPath = path.join(logPath, '..', '..', '..', '..', 'User', 'keybindings.json');
let userKeyBindings = [];

try {
userKeyBindings = await workspace.openTextDocument(keybindingsPath).then(document =>
JSON.parse(
document
.getText()
.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, '')
)
);
} catch { }

const uri = HelpView.encodeLocation(repository);
views.set(uri.toString(), new HelpView(uri));
views.set(uri.toString(), new HelpView(uri, userKeyBindings));
workspace.openTextDocument(uri).then(doc => window.showTextDocument(doc, { viewColumn: MagitUtils.oppositeActiveViewColumn(), preserveFocus: true, preview: false }));
}
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const views: Map<string, DocumentView> = new Map<string, DocumentView>();
export const processLog: MagitProcessLogEntry[] = [];

export let gitApi: API;
export let logPath: string;

export function activate(context: ExtensionContext) {

Expand All @@ -59,6 +60,7 @@ export function activate(context: ExtensionContext) {
}));

gitApi = gitExtension.getAPI(1);
logPath = context.logPath;

context.subscriptions.push(gitApi.onDidCloseRepository(repository => {
magitRepositories.delete(repository.rootUri.fsPath);
Expand Down
97 changes: 57 additions & 40 deletions src/views/helpView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,28 @@ import * as Constants from '../common/constants';
import { TextView } from './general/textView';
import { MagitRepository } from '../models/magitRepository';
import { MagitState } from '../models/magitState';
import * as meta from '../../package.json';

export class HelpView extends DocumentView {

static UriPath: string = 'help.magit';
isHighlightable = false;
needsUpdate = false;

// static HelpText: string = `Popup and dwim commands
// A Cherry-picking b Branching B Bisecting c Committing
// d Diffing D Change diffs e Ediff dwimming E Ediffing
// f Fetching F Pulling l Logging L Change logs
// m Merging M Remoting o Submodules O Subtrees
// P Pushing r Rebasing t Tagging T Notes
// V Reverting w Apply patches W Format patches X Resetting
// y Show Refs z Stashing ! Running % Worktree

// Applying changes
// a Apply s Stage u Unstage
// v Reverse S Stage all U Unstage all
// k Discard

// Essential commands
// g refresh current buffer
// TAB toggle section at point
// RET visit thing at point
// C-h m show all key bindings`;
constructor(uri: Uri, userKeyBindings: any) {
super(uri);

static HelpText: string = `Popup and dwim commands
A Cherry-pick b Branch c Commit
d Diff f Fetch F Pull
i Ignore l Log m Merge
M Remote P Push r Rebase
t Tag V Revert X Reset
y Show Refs z Stash ! Run % Worktree
Applying changes
a Apply s Stage u Unstage
v Reverse S Stage all U Unstage all
k Discard
Essential commands
g refresh current buffer
TAB toggle section at point
RET visit thing at point
$ show git process view`;
let binds = meta.contributes.keybindings.reduce((prev, bind) => ({
...prev,
[bind.command]: HelpView.keybindingToDisplayKey(bind.key)
}), {});

constructor(uri: Uri) {
super(uri);
binds = userKeyBindings.reduce((prev: any, bind: any) => ({
...prev,
[bind.command]: HelpView.keybindingToDisplayKey(bind.key)
}), binds);

const diffTextView = new TextView(HelpView.HelpText);
const diffTextView = new TextView(HelpView.createHelpText(binds));
diffTextView.isHighlightable = false;
this.addSubview(diffTextView);
}
Expand All @@ -63,4 +35,49 @@ Essential commands
static encodeLocation(repository: MagitRepository): Uri {
return Uri.parse(`${Constants.MagitUriScheme}:${HelpView.UriPath}?${repository.rootUri.path}#help`);
}

private static joinTexts(spacing: number, texts: string[]) {
let joinedText = texts.length > 0 ? texts[0] : '';
for (let i = 1; i < texts.length; i++) {
const prev = texts[i - 1];
const current = texts[i];

const remainingSpacing = prev.length <= spacing ? spacing - prev.length : 0;
joinedText += ' '.repeat(remainingSpacing) + current;
}
return joinedText;
}

private static keybindingToDisplayKey(bind: string): string {
if (/shift(\+|-)[a-zA-Z]/g.test(bind)) {
return bind.substring(6).toLocaleUpperCase();
} else if (bind === 'enter') {
return 'RET';
} else if (bind === 'tab') {
return 'TAB';
}
return bind;
}

private static createHelpText(c: any) {
return `Popup and dwim commands
${HelpView.joinTexts(19, [`${c['magit.cherry-picking']} Cherry-pick`, `${c['magit.branching']} Branch`, `${c['magit.commit']} Commit`])}
${HelpView.joinTexts(19, [`${c['magit.diffing']} Diff`, `${c['magit.fetching']} Fetch`, `${c['magit.pulling']} Pull`])}
${HelpView.joinTexts(19, [`${c['magit.ignoring']} Ignore`, `${c['magit.logging']} Log`, `${c['magit.merging']} Merge`])}
${HelpView.joinTexts(19, [`${c['magit.remoting']} Remote`, `${c['magit.pushing']} Push`, `${c['magit.rebasing']} Rebase`])}
${HelpView.joinTexts(19, [`${c['magit.tagging']} Tag`, `${c['magit.reverting']} Revert`, `${c['magit.resetting']} Reset`])}
${HelpView.joinTexts(19, [`${c['magit.show-refs']} Show Refs`, `${c['magit.stashing']} Stash`, `${c['magit.running']} Run`])}
${c['magit.worktree']} Worktree
Applying changes
${HelpView.joinTexts(17, [`${c['magit.apply-at-point']} Apply`, `${c['magit.stage']} Stage`, `${c['magit.unstage']} Unstage`])}
${HelpView.joinTexts(17, [`${c['magit.reverse-at-point']} Reverse`, `${c['magit.stage-all']} Stage all`, `${c['magit.unstage-all']} Unstage all`])}
${c['magit.discard-at-point']} Discard
Essential commands
${HelpView.joinTexts(9, [c['magit.refresh'], 'refresh current buffer'])}
${HelpView.joinTexts(9, [c['magit.toggle-fold'], 'toggle section at point'])}
${HelpView.joinTexts(9, [c['magit.visit-at-point'], 'visit thing at point'])}
${HelpView.joinTexts(9, [c['magit.process-log'], 'show git process view'])}`;
}
}
2 changes: 1 addition & 1 deletion syntaxes/magit.tmGrammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
]
},
"helpKey": {
"match": " ([a-zA-Z]|!|\\$|\\%|TAB|RET|\\_) ",
"match": " ([a-zA-Z]|!|\\$|\\%|TAB|RET|\\_|-|(shift(\\+|-)[^a-zA-Z])) ",
"name": "strong keyword.operator.new"
}
}
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"es6"
],
"sourceMap": true,
"rootDir": "src",
"rootDir": "",
"resolveJsonModule": true,
"strict": true /* enable all strict type-checking options */
/* Additional Checks */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const config = {
use: [
{
options: {

configFile: 'tsconfig.json'
},
loader: 'ts-loader'
}
Expand Down

0 comments on commit 3660bfa

Please sign in to comment.