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 translations in diagnostics, update translation domain #675

Merged
merged 3 commits into from
Oct 9, 2021
Merged
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
2 changes: 1 addition & 1 deletion packages/completion-theme/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export const COMPLETION_THEME_MANAGER: JupyterFrontEndPlugin<ILSPCompletionTheme
commandPalette: ICommandPalette,
translator: ITranslator
) => {
const trans = translator.load('jupyterlab-lsp');
const trans = translator.load('jupyterlab_lsp');
let manager = new CompletionThemeManager(themeManager, trans);
const command_id = 'lsp:completer-about-themes';
app.commands.addCommand(command_id, {
Expand Down
36 changes: 25 additions & 11 deletions packages/jupyterlab-lsp/src/components/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { IDocumentWidget } from '@jupyterlab/docregistry';
import { nullTranslator } from '@jupyterlab/translation';

import { WidgetAdapter } from '../adapters/adapter';
import { BrowserConsole } from '../virtual/console';
import { VirtualDocument } from '../virtual/document';

import { get_breadcrumbs } from './utils';
import { getBreadcrumbs } from './utils';

function create_dummy_document(options: Partial<VirtualDocument.IOptions>) {
return new VirtualDocument({
Expand All @@ -19,14 +20,19 @@ function create_dummy_document(options: Partial<VirtualDocument.IOptions>) {
});
}

describe('get_breadcrumbs', () => {
describe('getBreadcrumbs', () => {
const trans = nullTranslator.load('jupyterlab_lsp');
it('should collapse long paths', () => {
let document = create_dummy_document({
path: 'dir1/dir2/Test.ipynb'
});
let breadcrumbs = get_breadcrumbs(document, {
has_multiple_editors: false
} as WidgetAdapter<IDocumentWidget>);
let breadcrumbs = getBreadcrumbs(
document,
{
has_multiple_editors: false
} as WidgetAdapter<IDocumentWidget>,
trans
);
expect(breadcrumbs[0].props['title']).toBe('dir1/dir2/Test.ipynb');
expect(breadcrumbs[0].props['children']).toBe('dir1/.../Test.ipynb');
});
Expand All @@ -37,9 +43,13 @@ describe('get_breadcrumbs', () => {
file_extension: 'py',
has_lsp_supported_file: false
});
let breadcrumbs = get_breadcrumbs(document, {
has_multiple_editors: false
} as WidgetAdapter<IDocumentWidget>);
let breadcrumbs = getBreadcrumbs(
document,
{
has_multiple_editors: false
} as WidgetAdapter<IDocumentWidget>,
trans
);
expect(breadcrumbs[0].props['children']).toBe('Test.ipynb');
});

Expand All @@ -49,9 +59,13 @@ describe('get_breadcrumbs', () => {
file_extension: 'py',
has_lsp_supported_file: true
});
let breadcrumbs = get_breadcrumbs(document, {
has_multiple_editors: false
} as WidgetAdapter<IDocumentWidget>);
let breadcrumbs = getBreadcrumbs(
document,
{
has_multiple_editors: false
} as WidgetAdapter<IDocumentWidget>,
trans
);
expect(breadcrumbs[0].props['children']).toBe('test.py');
});
});
25 changes: 21 additions & 4 deletions packages/jupyterlab-lsp/src/components/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { IDocumentWidget } from '@jupyterlab/docregistry';
import { nullTranslator, TranslationBundle } from '@jupyterlab/translation';
import React from 'react';

import { WidgetAdapter } from '../adapters/adapter';
import { VirtualDocument } from '../virtual/document';

export function get_breadcrumbs(
export function getBreadcrumbs(
document: VirtualDocument,
adapter: WidgetAdapter<IDocumentWidget>,
trans?: TranslationBundle,
collapse = true
): JSX.Element[] {
if (!trans) {
trans = nullTranslator.load('');
}
return document.ancestry.map((document: VirtualDocument) => {
if (!document.parent) {
let path = document.path;
Expand Down Expand Up @@ -46,8 +51,8 @@ export function get_breadcrumbs(

let cell_locator =
first_cell === last_cell
? `cell ${first_cell + 1}`
: `cells: ${first_cell + 1}-${last_cell + 1}`;
? trans.__('cell %1', first_cell + 1)
: trans.__('cells: %1-%2', first_cell + 1, last_cell + 1);

return (
<span key={document.uri}>
Expand All @@ -62,6 +67,17 @@ export function get_breadcrumbs(
});
}

/**
* @deprecated please use getBreadcrumbs instead; `get_breadcrumbs` will be removed in 4.0
*/
export function get_breadcrumbs(
document: VirtualDocument,
adapter: WidgetAdapter<IDocumentWidget>,
collapse = true
) {
return getBreadcrumbs(document, adapter, null, collapse);
}

export function focus_on(node: HTMLElement) {
if (!node) {
return;
Expand All @@ -73,6 +89,7 @@ export function focus_on(node: HTMLElement) {
export function DocumentLocator(props: {
document: VirtualDocument;
adapter: WidgetAdapter<any>;
trans?: TranslationBundle;
}) {
let { document, adapter } = props;
let target: HTMLElement = null;
Expand All @@ -84,7 +101,7 @@ export function DocumentLocator(props: {
console.warn('Could not get first line of ', document);
}
}
let breadcrumbs = get_breadcrumbs(document, adapter);
let breadcrumbs = getBreadcrumbs(document, adapter, props.trans);
return (
<div
className={'lsp-document-locator'}
Expand Down
40 changes: 23 additions & 17 deletions packages/jupyterlab-lsp/src/features/diagnostics/diagnostics.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { JupyterFrontEnd } from '@jupyterlab/application';
import { MainAreaWidget } from '@jupyterlab/apputils';
import { TranslationBundle } from '@jupyterlab/translation';
import { nullTranslator, TranslationBundle } from '@jupyterlab/translation';
import { LabIcon, copyIcon } from '@jupyterlab/ui-components';
import { Menu } from '@lumino/widgets';
import type * as CodeMirror from 'codemirror';
Expand Down Expand Up @@ -56,12 +56,16 @@ class DiagnosticsPanel {
is_registered = false;
trans: TranslationBundle;

constructor(trans: TranslationBundle) {
this.trans = trans;
}

get widget() {
if (this._widget == null || this._widget.content.model == null) {
if (this._widget && !this._widget.isDisposed) {
this._widget.dispose();
}
this._widget = this.init_widget();
this._widget = this.initWidget();
}
return this._widget;
}
Expand All @@ -70,15 +74,15 @@ class DiagnosticsPanel {
return this.widget.content;
}

protected init_widget() {
protected initWidget() {
this._content = new DiagnosticsListing(
new DiagnosticsListing.Model(this.trans)
);
this._content.model.diagnostics = new DiagnosticsDatabase();
this._content.addClass('lsp-diagnostics-panel-content');
const widget = new MainAreaWidget({ content: this._content });
widget.id = 'lsp-diagnostics-panel';
widget.title.label = this.trans?.__('Diagnostics Panel');
widget.title.label = this.trans.__('Diagnostics Panel');
widget.title.closable = true;
widget.title.icon = diagnosticsIcon;
return widget;
Expand All @@ -95,10 +99,10 @@ class DiagnosticsPanel {
register(app: JupyterFrontEnd) {
const widget = this.widget;

let get_column = (name: string) => {
let get_column = (id: string) => {
// TODO: a hashmap in the panel itself?
for (let column of widget.content.columns) {
if (column.name === name) {
if (column.id === id) {
return column;
}
}
Expand All @@ -110,21 +114,21 @@ class DiagnosticsPanel {

app.commands.addCommand(CMD_COLUMN_VISIBILITY, {
execute: args => {
let column = get_column(args['name'] as string);
let column = get_column(args['id'] as string);
column.is_visible = !column.is_visible;
widget.update();
},
label: args => this.trans.__(`${args['name']}`) as string,
label: args => this.trans.__(args['id'] as string),
isToggled: args => {
let column = get_column(args['name'] as string);
let column = get_column(args['id'] as string);
return column.is_visible;
}
});

for (let column of widget.content.columns) {
columns_menu.addItem({
command: CMD_COLUMN_VISIBILITY,
args: { name: column.name }
args: { id: column.id }
});
}
app.contextMenu.addItem({
Expand Down Expand Up @@ -238,7 +242,7 @@ class DiagnosticsPanel {
.writeText(message)
.then(() => {
this.content.model.status_message.set(
this.trans.__(`Successfully copied "%1" to clipboard`, message)
this.trans.__('Successfully copied "%1" to clipboard', message)
);
})
.catch(() => {
Expand Down Expand Up @@ -275,7 +279,9 @@ class DiagnosticsPanel {
}
}

export const diagnostics_panel = new DiagnosticsPanel();
export const diagnostics_panel = new DiagnosticsPanel(
nullTranslator.load('jupyterlab_lsp')
);
export const diagnostics_databases = new WeakMap<
CodeMirrorVirtualEditor,
DiagnosticsDatabase
Expand Down Expand Up @@ -349,7 +355,7 @@ export class DiagnosticsCM extends CodeMirrorIntegration {
diagnostics_panel.update();
};

protected collapse_overlapping_diagnostics(
protected collapseOverlappingDiagnostics(
diagnostics: lsProtocol.Diagnostic[]
): Map<lsProtocol.Range, lsProtocol.Diagnostic[]> {
// because Range is not a primitive type, the equality of the objects having
Expand Down Expand Up @@ -447,7 +453,7 @@ export class DiagnosticsCM extends CodeMirrorIntegration {

// TODO: test case for severity class always being set, even if diagnostic has no severity

let diagnostics_by_range = this.collapse_overlapping_diagnostics(
let diagnostics_by_range = this.collapseOverlappingDiagnostics(
this.filterDiagnostics(response.diagnostics)
);

Expand Down Expand Up @@ -607,7 +613,7 @@ export class DiagnosticsCM extends CodeMirrorIntegration {
);

// remove the markers which were not included in the new message
this.remove_unused_diagnostic_markers(markers_to_retain);
this.removeUnusedDiagnosticMarkers(markers_to_retain);

this.diagnostics_db.set(this.virtual_document, diagnostics_list);
}
Expand Down Expand Up @@ -641,7 +647,7 @@ export class DiagnosticsCM extends CodeMirrorIntegration {
diagnostics_panel.update();
}

protected remove_unused_diagnostic_markers(to_retain: Set<string>) {
protected removeUnusedDiagnosticMarkers(to_retain: Set<string>) {
this.marked_diagnostics.forEach(
(marker: CodeMirror.TextMarker, diagnostic_hash: string) => {
if (!to_retain.has(diagnostic_hash)) {
Expand All @@ -655,7 +661,7 @@ export class DiagnosticsCM extends CodeMirrorIntegration {
remove(): void {
this.settings.changed.disconnect(this.refreshDiagnostics, this);
// remove all markers
this.remove_unused_diagnostic_markers(new Set());
this.removeUnusedDiagnosticMarkers(new Set());
this.diagnostics_db.clear();
diagnostics_databases.delete(this.virtual_editor);
this.unique_editor_ids.clear();
Expand Down
3 changes: 2 additions & 1 deletion packages/jupyterlab-lsp/src/features/diagnostics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const COMMANDS = (trans: TranslationBundle): IFeatureCommand[] => [
diagnostics_feature.switchDiagnosticsPanelSource();

if (!diagnostics_panel.is_registered) {
diagnostics_panel.trans = trans;
diagnostics_panel.register(app);
}

Expand Down Expand Up @@ -56,7 +57,7 @@ export const DIAGNOSTICS_PLUGIN: JupyterFrontEndPlugin<void> = {
translator: ITranslator
) => {
const settings = new FeatureSettings(settingRegistry, FEATURE_ID);
const trans = translator.load('jupyterlab-lsp');
const trans = translator.load('jupyterlab_lsp');

featureManager.register({
feature: {
Expand Down
Loading