Skip to content
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

Improve preference renderer linking #14311

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion packages/core/src/browser/core-preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ export const corePreferenceSchema: PreferenceSchema = {
nls.localizeByDefault('Menu is displayed at the top of the window and only hidden in full screen mode.'),
nls.localizeByDefault('Menu is always visible at the top of the window even in full screen mode.'),
nls.localizeByDefault('Menu is always hidden.'),
nls.localizeByDefault('Menu is displayed as a compact button in the side bar. This value is ignored when {0} is {1}.', '`#window.titleBarStyle#`', '`native`')
environment.electron.is()
? nls.localizeByDefault('Menu is displayed as a compact button in the side bar. This value is ignored when {0} is {1}.', '`#window.titleBarStyle#`', '`native`')
: nls.localizeByDefault('Menu is displayed as a compact button in the side bar.')
],
default: 'classic',
scope: 'application',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ export const editorGeneratedPreferenceProperties: PreferenceSchema['properties']
nls.localizeByDefault("`cursorSurroundingLines` is enforced only when triggered via the keyboard or API."),
nls.localizeByDefault("`cursorSurroundingLines` is enforced always.")
],
"markdownDescription": nls.localize("theia/editor/editor.cursorSurroundingLinesStyle", "Controls when `#cursorSurroundingLines#` should be enforced."),
"markdownDescription": nls.localizeByDefault("Controls when `#editor.cursorSurroundingLines#` should be enforced."),
"type": "string",
"enum": [
"default",
Expand Down
5 changes: 5 additions & 0 deletions packages/preferences/src/browser/style/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@
text-decoration: none;
}

.theia-settings-container .disabled-link {
pointer-events: none;
cursor: default;
}

.theia-settings-container .settings-section a:hover {
text-decoration: underline;
}
Expand Down
18 changes: 12 additions & 6 deletions packages/preferences/src/browser/util/preference-tree-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class PreferenceTreeGenerator {
@inject(PreferenceLayoutProvider) protected readonly layoutProvider: PreferenceLayoutProvider;

protected _root: CompositeTreeNode;
protected _idCache = new Map<string, string>();

protected readonly onSchemaChangedEmitter = new Emitter<CompositeTreeNode>();
readonly onSchemaChanged = this.onSchemaChangedEmitter.event;
Expand All @@ -60,6 +61,7 @@ export class PreferenceTreeGenerator {
}

generateTree(): CompositeTreeNode {
this._idCache.clear();
const preferencesSchema = this.schemaProvider.getCombinedSchema();
const propertyNames = Object.keys(preferencesSchema.properties);
const groups = new Map<string, Preference.CompositeTreeNode>();
Expand Down Expand Up @@ -121,22 +123,23 @@ export class PreferenceTreeGenerator {

protected createBuiltinLeafNode(name: string, property: PreferenceDataProperty, root: CompositeTreeNode, groups: Map<string, Preference.CompositeTreeNode>): void {
const layoutItem = this.layoutProvider.getLayoutForPreference(name);
const labels = layoutItem ? layoutItem.id.split('.') : name.split('.');
const labels = (layoutItem?.id ?? name).split('.');
const groupID = this.getGroupName(labels);
const subgroupName = this.getSubgroupName(labels, groupID);
const subgroupID = [groupID, subgroupName].join('.');
const toplevelParent = this.getOrCreatePreferencesGroup({
id: groupID,
group: groupID,
root,
groups
groups,
label: this.generateName(groupID)
});
const immediateParent = subgroupName ? this.getOrCreatePreferencesGroup({
id: subgroupID,
group: groupID,
root: toplevelParent,
groups,
label: layoutItem?.label
label: layoutItem?.label ?? this.generateName(subgroupName)
}) : undefined;
this.createLeafNode(name, immediateParent || toplevelParent, property);
}
Expand Down Expand Up @@ -177,9 +180,7 @@ export class PreferenceTreeGenerator {
}

getNodeId(preferenceId: string): string {
const expectedGroup = this.getGroupName(preferenceId.split('.'));
const expectedId = `${expectedGroup}@${preferenceId}`;
return expectedId;
return this._idCache.get(preferenceId) ?? '';
}

protected getGroupName(labels: string[]): string {
Expand All @@ -200,6 +201,10 @@ export class PreferenceTreeGenerator {
}
}

protected generateName(id: string): string {
return id.substring(0, 1).toUpperCase() + id.substring(1);
}

doHandleChangedSchema(): void {
const newTree = this.generateTree();
this.onSchemaChangedEmitter.fire(newTree);
Expand All @@ -226,6 +231,7 @@ export class PreferenceTreeGenerator {
preference: { data },
depth: Preference.TreeNode.isTopLevel(preferencesGroup) ? 1 : 2
};
this._idCache.set(property, newNode.id);
CompositeTreeNode.addChild(preferencesGroup, newNode);
return newNode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ import { inject, injectable } from '@theia/core/shared/inversify';
import { PreferenceTreeModel } from '../../preference-tree-model';
import { PreferenceTreeLabelProvider } from '../../util/preference-tree-label-provider';
import * as markdownit from '@theia/core/shared/markdown-it';
import { CommandRegistry } from '@theia/core';

@injectable()
export class PreferenceMarkdownRenderer {

@inject(PreferenceTreeModel) protected readonly model: PreferenceTreeModel;
@inject(PreferenceTreeLabelProvider) protected readonly labelProvider: PreferenceTreeLabelProvider;
@inject(PreferenceTreeModel)
protected readonly model: PreferenceTreeModel;
@inject(PreferenceTreeLabelProvider)
protected readonly labelProvider: PreferenceTreeLabelProvider;
@inject(CommandRegistry)
protected readonly commandRegistry: CommandRegistry;

protected _renderer?: markdownit;

Expand All @@ -47,19 +52,26 @@ export class PreferenceMarkdownRenderer {
engine.renderer.rules.code_inline = (tokens, idx, options, env, self) => {
const token = tokens[idx];
const content = token.content;
if (content.startsWith('#') && content.endsWith('#')) {
const preferenceId = content.substring(1, content.length - 1);
const preferenceNode = this.model.getNodeFromPreferenceId(preferenceId);
if (content.length > 2 && content.startsWith('#') && content.endsWith('#')) {
const id = content.substring(1, content.length - 1);
// First check whether there's a preference with the given ID
const preferenceNode = this.model.getNodeFromPreferenceId(id);
if (preferenceNode) {
let name = this.labelProvider.getName(preferenceNode);
const prefix = this.labelProvider.getPrefix(preferenceNode, true);
if (prefix) {
name = prefix + name;
}
return `<a title="${preferenceId}" href="preference:${preferenceId}">${name}</a>`;
} else {
console.warn(`Linked preference "${preferenceId}" not found.`);
return `<a title="${id}" href="preference:${id}">${name}</a>`;
}
// If no preference was found, check whether there's a command with the given ID
const command = this.commandRegistry.getCommand(id);
if (command) {
const name = `${command.category ? `${command.category}: ` : ''}${command.label}`;
return `<a class="disabled-link" title="${id}">${name}</a>`;
msujew marked this conversation as resolved.
Show resolved Hide resolved
}
// If nothing was found, print a warning
console.warn(`Linked preference "${id}" not found.`);
}
return inlineCode ? inlineCode(tokens, idx, options, env, self) : '';
};
Expand Down
Loading