Skip to content

Custom formatting of display name #359

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

Open
wants to merge 1 commit 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
15 changes: 15 additions & 0 deletions package.build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,21 @@ export const pkg = (modules: Builder.ParsedModule[]) => ({
+ "non-directional, like Kakoune.",
],
},
activeModeDisplayTextTransform: {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This defines activeModeDisplayTextTransform as a property set on modes, not a global configuration. You need to move this below and name it "dance.activeModeDisplayTextTransform" instead.

"enum": [
"as-is",
"uppercase",
"lowercase",

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove empty line

Suggested change

],
"default": "as-is",
"description": "Controls how the active mode is formatted in the status bar.",
"enumDescriptions": [
"Display the name with its original formatting.",
"Convert the name to uppercase.",
"Convert the name to lowercase.",
],
},
decorations: {
...selectionDecorationType,
type: ["array", "object", "null"],
Expand Down
14 changes: 14 additions & 0 deletions package.json

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

19 changes: 16 additions & 3 deletions src/state/editors.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as vscode from "vscode";

import type { Extension } from "./extension";
import type { Mode } from "./modes";
import { command, commands, Context, Positions, SelectionBehavior, Selections } from "../api";
import { extensionName } from "../utils/constants";
import { assert } from "../utils/errors";
import type { Extension } from "./extension";
import type { Mode } from "./modes";

/**
* Dance-specific state related to a single `vscode.TextEditor`.
Expand Down Expand Up @@ -234,14 +234,27 @@ export class PerEditorState implements vscode.Disposable {
public notifyDidBecomeActive() {
const { editor, mode } = this;

this.extension.statusBar.activeModeSegment.setContent(mode.name);
this.extension.statusBar.activeModeSegment.setContent(this._formatDisplayName(mode.name));

editor.options.lineNumbers = mode.lineNumbers;
editor.options.cursorStyle = mode.cursorStyle;

return vscode.commands.executeCommand("setContext", extensionName + ".mode", mode.name);
}

private _formatDisplayName(modeName: string) {
switch (vscode.workspace.getConfiguration(extensionName)
.get<string>("activeModeDisplayTextTransform")) {
Comment on lines +246 to +247
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good way to get the key, but to avoid reading the configuration every time the mode changes (which can occur pretty often), I would instead use observePreference() in src/state/extension.ts to keep a field of Extension in sync with the configuration, and then read from _extension in notifyDidBecomeActive().

case "uppercase":
return modeName.toUpperCase();
case "lowercase":
return modeName.toLowerCase();
case "as-is":
default:
return modeName;
}
}

/**
* Called when `vscode.window.onDidChangeActiveTextEditor` is triggered with
* another editor.
Expand Down