Skip to content

Commit

Permalink
Slight refactor of StatusBar.Set()
Browse files Browse the repository at this point in the history
  • Loading branch information
J-Fields committed Nov 21, 2019
1 parent 7379d42 commit a81dd24
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 91 deletions.
3 changes: 1 addition & 2 deletions src/actions/commands/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4531,8 +4531,7 @@ class CommandUnicodeName extends BaseCommand {
// TODO: Handle charCode > 127 by also including <M-x>
StatusBar.Set(
`<${char}> ${charCode}, Hex ${charCode.toString(16)}, Octal ${charCode.toString(8)}`,
vimState.currentMode,
vimState.isRecordingMacro,
vimState,
true
);
return vimState;
Expand Down
11 changes: 3 additions & 8 deletions src/cmd_line/commandLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class CommandLine {
}

if ('help'.startsWith(command.split(/\s/)[0])) {
StatusBar.Set(`:help Not supported.`, vimState.currentMode, vimState.isRecordingMacro, true);
StatusBar.Set(`:help Not supported.`, vimState, true);
return;
}

Expand All @@ -81,7 +81,7 @@ class CommandLine {

if (useNeovim) {
const statusBarText = await vimState.nvim.run(vimState, command);
StatusBar.Set(statusBarText, vimState.currentMode, vimState.isRecordingMacro, true);
StatusBar.Set(statusBarText, vimState, true);
} else {
await cmd.execute(vimState.editor, vimState);
}
Expand All @@ -90,12 +90,7 @@ class CommandLine {
if (e.code === ErrorCode.E492 && configuration.enableNeovim) {
await vimState.nvim.run(vimState, command);
} else {
StatusBar.Set(
`${e.toString()}. ${command}`,
vimState.currentMode,
vimState.isRecordingMacro,
true
);
StatusBar.Set(`${e.toString()}. ${command}`, vimState, true);
}
} else {
this._logger.error(`Error executing cmd=${command}. err=${e}.`);
Expand Down
2 changes: 1 addition & 1 deletion src/cmd_line/commands/nohl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ export class NohlCommand extends node.CommandBase {
globalState.hl = false;

// Clear the `match x of y` message from status bar
StatusBar.Set('', vimState.currentMode, vimState.isRecordingMacro, true);
StatusBar.Set('', vimState, true);
}
}
13 changes: 3 additions & 10 deletions src/cmd_line/commands/setoptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ export class SetOptionsCommand extends node.CommandBase {
if (configuration[this._arguments.name] == null) {
StatusBar.Set(
`${VimError.fromCode(ErrorCode.E518).toString()}. ${this._arguments.name}`,
vimState.currentMode,
vimState.isRecordingMacro,
vimState,
true
);
return;
Expand Down Expand Up @@ -108,17 +107,11 @@ export class SetOptionsCommand extends node.CommandBase {
if (value === undefined) {
StatusBar.Set(
`${VimError.fromCode(ErrorCode.E518).toString()}. ${value}`,
vimState.currentMode,
vimState.isRecordingMacro,
vimState,
true
);
} else {
StatusBar.Set(
`${this._arguments.name}=${value}`,
vimState.currentMode,
vimState.isRecordingMacro,
true
);
StatusBar.Set(`${this._arguments.name}=${value}`, vimState, true);
}
break;
default:
Expand Down
8 changes: 4 additions & 4 deletions src/cmd_line/commands/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ export class WriteCommand extends node.CommandBase {
await promisify(fs.chmod)(vimState.editor.document.fileName, 666);
return this.save(vimState);
} catch (e) {
StatusBar.Set(e.message, vimState.currentMode, vimState.isRecordingMacro, true);
StatusBar.Set(e.message, vimState, true);
}
} else {
StatusBar.Set(accessErr.message, vimState.currentMode, vimState.isRecordingMacro, true);
StatusBar.Set(accessErr.message, vimState, true);
}
}
}
Expand All @@ -86,9 +86,9 @@ export class WriteCommand extends node.CommandBase {
'L ' +
vimState.editor.document.getText().length +
'C written';
StatusBar.Set(text, vimState.currentMode, vimState.isRecordingMacro, true);
StatusBar.Set(text, vimState, true);
},
e => StatusBar.Set(e, vimState.currentMode, vimState.isRecordingMacro, true)
e => StatusBar.Set(e, vimState, true)
)
);
}
Expand Down
7 changes: 1 addition & 6 deletions src/configuration/remapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,7 @@ export class Remapper implements IRemapper {
await vscode.commands.executeCommand(commandString);
}

StatusBar.Set(
commandString + ' ' + commandArgs,
vimState.currentMode,
vimState.isRecordingMacro,
true
);
StatusBar.Set(commandString + ' ' + commandArgs, vimState, true);
}
}
}
Expand Down
10 changes: 2 additions & 8 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,7 @@ export class ModeHandler implements vscode.Disposable {
}
} catch (e) {
if (e instanceof VimError) {
StatusBar.Set(
e.toString(),
this.vimState.currentMode,
this.vimState.isRecordingMacro,
true,
true
);
StatusBar.Set(e.toString(), this.vimState, true, true);
} else {
throw new Error(`Failed to handle key=${key}. ${e.message}`);
}
Expand Down Expand Up @@ -1435,7 +1429,7 @@ export class ModeHandler implements vscode.Disposable {
text.push(macroText);
}

StatusBar.Set(text.join(' '), this.currentMode.name, this.vimState.isRecordingMacro);
StatusBar.Set(text.join(' '), this.vimState);
}

async handleMultipleKeyEvents(keys: string[]): Promise<void> {
Expand Down
25 changes: 10 additions & 15 deletions src/statusBar.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as vscode from 'vscode';
import { ModeName } from './mode/mode';
import { configuration } from './configuration/configuration';
import { VimState } from './state/vimState';

class StatusBarImpl implements vscode.Disposable {
private _statusBarItem: vscode.StatusBarItem;
Expand All @@ -13,21 +14,15 @@ class StatusBarImpl implements vscode.Disposable {
this._statusBarItem.show();
}

public Set(
text: string,
mode: ModeName,
isRecordingMacro: boolean,
isHighPriority = false,
isError = false
) {
const hasModeChanged = mode !== this._previousModeName;
public Set(text: string, vimState: VimState, isHighPriority = false, isError = false) {
const hasModeChanged = vimState.currentMode !== this._previousModeName;

// text
const shouldUpdateText =
hasModeChanged ||
mode === ModeName.SearchInProgressMode ||
mode === ModeName.CommandlineInProgress ||
isRecordingMacro !== this._wasRecordingMacro ||
vimState.currentMode === ModeName.SearchInProgressMode ||
vimState.currentMode === ModeName.CommandlineInProgress ||
vimState.isRecordingMacro !== this._wasRecordingMacro ||
configuration.showcmd;

// Errors and other high-priority messages remain displayed on the status bar
Expand All @@ -42,17 +37,17 @@ class StatusBarImpl implements vscode.Disposable {
// color
const shouldUpdateColor = configuration.statusBarColorControl && hasModeChanged;
if (shouldUpdateColor) {
this.UpdateColor(mode);
this.UpdateColor(vimState.currentMode);
}

if (hasModeChanged && mode !== ModeName.Normal) {
if (hasModeChanged && vimState.currentMode !== ModeName.Normal) {
this._wasHighPriority = false;
} else if (isHighPriority) {
this._wasHighPriority = true;
}

this._previousModeName = mode;
this._wasRecordingMacro = isRecordingMacro;
this._previousModeName = vimState.currentMode;
this._wasRecordingMacro = vimState.isRecordingMacro;
}

dispose() {
Expand Down
44 changes: 7 additions & 37 deletions src/util/statusBarTextUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { configuration } from '../configuration/configuration';
import { Position } from '../common/motion/position';

export function ReportClear(vimState: VimState) {
StatusBar.Set('', vimState.currentMode, vimState.isRecordingMacro, true);
StatusBar.Set('', vimState, true);
}

/**
Expand All @@ -15,19 +15,9 @@ export function ReportClear(vimState: VimState) {
*/
export function ReportLinesChanged(numLinesChanged: number, vimState: VimState) {
if (numLinesChanged > configuration.report) {
StatusBar.Set(
numLinesChanged + ' more lines',
vimState.currentMode,
vimState.isRecordingMacro,
true
);
StatusBar.Set(numLinesChanged + ' more lines', vimState, true);
} else if (-numLinesChanged > configuration.report) {
StatusBar.Set(
Math.abs(numLinesChanged) + ' fewer lines',
vimState.currentMode,
vimState.isRecordingMacro,
true
);
StatusBar.Set(Math.abs(numLinesChanged) + ' fewer lines', vimState, true);
} else {
ReportClear(vimState);
}
Expand All @@ -40,19 +30,9 @@ export function ReportLinesChanged(numLinesChanged: number, vimState: VimState)
export function ReportLinesYanked(numLinesYanked: number, vimState: VimState) {
if (numLinesYanked > configuration.report) {
if (vimState.currentMode === ModeName.VisualBlock) {
StatusBar.Set(
'block of ' + numLinesYanked + ' lines yanked',
vimState.currentMode,
vimState.isRecordingMacro,
true
);
StatusBar.Set('block of ' + numLinesYanked + ' lines yanked', vimState, true);
} else {
StatusBar.Set(
numLinesYanked + ' lines yanked',
vimState.currentMode,
vimState.isRecordingMacro,
true
);
StatusBar.Set(numLinesYanked + ' lines yanked', vimState, true);
}
} else {
ReportClear(vimState);
Expand All @@ -67,12 +47,7 @@ export function ReportFileInfo(position: Position, vimState: VimState) {
const doc = vimState.editor.document;
const progress = Math.floor(((position.line + 1) / doc.lineCount) * 100);

StatusBar.Set(
`"${doc.fileName}" ${doc.lineCount} lines --${progress}%--`,
vimState.currentMode,
vimState.isRecordingMacro,
true
);
StatusBar.Set(`"${doc.fileName}" ${doc.lineCount} lines --${progress}%--`, vimState, true);
}

/**
Expand All @@ -82,10 +57,5 @@ export function ReportFileInfo(position: Position, vimState: VimState) {
* @param vimState The current `VimState`
*/
export function ReportSearch(matchIdx: number, numMatches: number, vimState: VimState) {
StatusBar.Set(
`match ${matchIdx + 1} of ${numMatches}`,
vimState.currentMode,
vimState.isRecordingMacro,
true
);
StatusBar.Set(`match ${matchIdx + 1} of ${numMatches}`, vimState, true);
}

0 comments on commit a81dd24

Please sign in to comment.