Use eslint&prettier for code linting&formatting

This commit is contained in:
Francesco Stasi
2021-06-16 15:08:48 +02:00
committed by Francesco Stasi
parent 2a3873a923
commit 0592199858
173 changed files with 8963 additions and 3841 deletions

View File

@@ -2,8 +2,14 @@ import { inject, injectable } from 'inversify';
import URI from '@theia/core/lib/common/uri';
import { open } from '@theia/core/lib/browser/opener-service';
import { FileStat } from '@theia/filesystem/lib/common/files';
import { CommandRegistry, CommandService } from '@theia/core/lib/common/command';
import { WorkspaceCommandContribution as TheiaWorkspaceCommandContribution, WorkspaceCommands } from '@theia/workspace/lib/browser/workspace-commands';
import {
CommandRegistry,
CommandService,
} from '@theia/core/lib/common/command';
import {
WorkspaceCommandContribution as TheiaWorkspaceCommandContribution,
WorkspaceCommands,
} from '@theia/workspace/lib/browser/workspace-commands';
import { Sketch } from '../../../common/protocol';
import { WorkspaceInputDialog } from './workspace-input-dialog';
import { SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl';
@@ -12,7 +18,6 @@ import { SingleTextInputDialog } from '@theia/core/lib/browser';
@injectable()
export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribution {
@inject(SketchesServiceClientImpl)
protected readonly sketchesServiceClient: SketchesServiceClientImpl;
@@ -22,13 +27,19 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
registerCommands(registry: CommandRegistry): void {
super.registerCommands(registry);
registry.unregisterCommand(WorkspaceCommands.NEW_FILE);
registry.registerCommand(WorkspaceCommands.NEW_FILE, this.newWorkspaceRootUriAwareCommandHandler({
execute: uri => this.newFile(uri)
}));
registry.registerCommand(
WorkspaceCommands.NEW_FILE,
this.newWorkspaceRootUriAwareCommandHandler({
execute: (uri) => this.newFile(uri),
})
);
registry.unregisterCommand(WorkspaceCommands.FILE_RENAME);
registry.registerCommand(WorkspaceCommands.FILE_RENAME, this.newUriAwareCommandHandler({
execute: uri => this.renameFile(uri)
}));
registry.registerCommand(
WorkspaceCommands.FILE_RENAME,
this.newUriAwareCommandHandler({
execute: (uri) => this.renameFile(uri),
})
);
}
protected async newFile(uri: URI | undefined): Promise<void> {
@@ -41,11 +52,14 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
}
const parentUri = parent.resource;
const dialog = new WorkspaceInputDialog({
title: 'Name for new file',
parentUri,
validate: name => this.validateFileName(name, parent, true)
}, this.labelProvider);
const dialog = new WorkspaceInputDialog(
{
title: 'Name for new file',
parentUri,
validate: (name) => this.validateFileName(name, parent, true),
},
this.labelProvider
);
const name = await dialog.open();
const nameWithExt = this.maybeAppendInoExt(name);
@@ -57,12 +71,20 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
}
}
protected async validateFileName(name: string, parent: FileStat, recursive: boolean = false): Promise<string> {
protected async validateFileName(
name: string,
parent: FileStat,
recursive = false
): Promise<string> {
// In the Java IDE the followings are the rules:
// - `name` without an extension should default to `name.ino`.
// - `name` with a single trailing `.` also defaults to `name.ino`.
const nameWithExt = this.maybeAppendInoExt(name);
const errorMessage = await super.validateFileName(nameWithExt, parent, recursive);
const errorMessage = await super.validateFileName(
nameWithExt,
parent,
recursive
);
if (errorMessage) {
return errorMessage;
}
@@ -82,10 +104,10 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
}
if (name.trim().length) {
if (name.indexOf('.') === -1) {
return `${name}.ino`
return `${name}.ino`;
}
if (name.lastIndexOf('.') === name.length - 1) {
return `${name.slice(0, -1)}.ino`
return `${name.slice(0, -1)}.ino`;
}
}
return name;
@@ -103,9 +125,12 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
const options = {
execOnlyIfTemp: false,
openAfterMove: true,
wipeOriginal: true
wipeOriginal: true,
};
await this.commandService.executeCommand(SaveAsSketch.Commands.SAVE_AS_SKETCH.id, options);
await this.commandService.executeCommand(
SaveAsSketch.Commands.SAVE_AS_SKETCH.id,
options
);
return;
}
const parent = await this.getParent(uri);
@@ -118,14 +143,14 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
initialValue,
initialSelectionRange: {
start: 0,
end: uri.path.name.length
end: uri.path.name.length,
},
validate: (name, mode) => {
if (initialValue === name && mode === 'preview') {
return false;
}
return this.validateFileName(name, parent, false);
}
},
});
const newName = await dialog.open();
const newNameWithExt = this.maybeAppendInoExt(newName);
@@ -135,5 +160,4 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
this.fileService.move(oldUri, newUri);
}
}
}

View File

@@ -6,7 +6,6 @@ import { SketchesServiceClientImpl } from '../../../common/protocol/sketches-ser
@injectable()
export class WorkspaceDeleteHandler extends TheiaWorkspaceDeleteHandler {
@inject(SketchesServiceClientImpl)
protected readonly sketchesServiceClient: SketchesServiceClientImpl;
@@ -16,15 +15,26 @@ export class WorkspaceDeleteHandler extends TheiaWorkspaceDeleteHandler {
return;
}
// Deleting the main sketch file.
if (uris.map(uri => uri.toString()).some(uri => uri === sketch.mainFileUri)) {
if (
uris
.map((uri) => uri.toString())
.some((uri) => uri === sketch.mainFileUri)
) {
const { response } = await remote.dialog.showMessageBox({
title: 'Delete',
type: 'question',
buttons: ['Cancel', 'OK'],
message: 'Do you want to delete the current sketch?'
message: 'Do you want to delete the current sketch?',
});
if (response === 1) { // OK
await Promise.all([...sketch.additionalFileUris, ...sketch.otherSketchFileUris, sketch.mainFileUri].map(uri => this.closeWithoutSaving(new URI(uri))));
if (response === 1) {
// OK
await Promise.all(
[
...sketch.additionalFileUris,
...sketch.otherSketchFileUris,
sketch.mainFileUri,
].map((uri) => this.closeWithoutSaving(new URI(uri)))
);
await this.fileService.delete(new URI(sketch.uri));
window.close();
}
@@ -32,5 +42,4 @@ export class WorkspaceDeleteHandler extends TheiaWorkspaceDeleteHandler {
}
return super.execute(uris);
}
}

View File

@@ -2,12 +2,14 @@ import { injectable } from 'inversify';
import { CommandRegistry } from '@theia/core/lib/common/command';
import { MenuModelRegistry } from '@theia/core/lib/common/menu';
import { KeybindingRegistry } from '@theia/core/lib/browser/keybinding';
import { WorkspaceCommands, FileMenuContribution } from '@theia/workspace/lib/browser/workspace-commands';
import {
WorkspaceCommands,
FileMenuContribution,
} from '@theia/workspace/lib/browser/workspace-commands';
import { WorkspaceFrontendContribution as TheiaWorkspaceFrontendContribution } from '@theia/workspace/lib/browser/workspace-frontend-contribution';
@injectable()
export class WorkspaceFrontendContribution extends TheiaWorkspaceFrontendContribution {
registerCommands(registry: CommandRegistry): void {
super.registerCommands(registry);
// TODO: instead of blacklisting commands to remove, it would be more robust to whitelist the ones we want to keep
@@ -20,29 +22,29 @@ export class WorkspaceFrontendContribution extends TheiaWorkspaceFrontendContrib
WorkspaceCommands.OPEN_RECENT_WORKSPACE,
WorkspaceCommands.SAVE_WORKSPACE_AS,
WorkspaceCommands.SAVE_AS,
WorkspaceCommands.CLOSE
].filter(commands.has.bind(commands)).forEach(registry.unregisterCommand.bind(registry));
WorkspaceCommands.CLOSE,
]
.filter(commands.has.bind(commands))
.forEach(registry.unregisterCommand.bind(registry));
}
registerMenus(_: MenuModelRegistry): void {
}
registerMenus(_: MenuModelRegistry): void {}
registerKeybindings(registry: KeybindingRegistry): void {
super.registerKeybindings(registry);
[
WorkspaceCommands.NEW_FILE,
WorkspaceCommands.FILE_RENAME,
WorkspaceCommands.FILE_DELETE
].map(({ id }) => id).forEach(registry.unregisterKeybinding.bind(registry));
WorkspaceCommands.FILE_DELETE,
]
.map(({ id }) => id)
.forEach(registry.unregisterKeybinding.bind(registry));
}
}
@injectable()
export class ArduinoFileMenuContribution extends FileMenuContribution {
registerMenus(_: MenuModelRegistry): void {
// NOOP
}
}

View File

@@ -2,15 +2,18 @@ import { inject } from 'inversify';
import { MaybePromise } from '@theia/core/lib/common/types';
import { LabelProvider } from '@theia/core/lib/browser/label-provider';
import { DialogError, DialogMode } from '@theia/core/lib/browser/dialogs';
import { WorkspaceInputDialog as TheiaWorkspaceInputDialog, WorkspaceInputDialogProps } from '@theia/workspace/lib/browser/workspace-input-dialog';
import {
WorkspaceInputDialog as TheiaWorkspaceInputDialog,
WorkspaceInputDialogProps,
} from '@theia/workspace/lib/browser/workspace-input-dialog';
export class WorkspaceInputDialog extends TheiaWorkspaceInputDialog {
protected wasTouched = false;
constructor(
@inject(WorkspaceInputDialogProps) protected readonly props: WorkspaceInputDialogProps,
@inject(LabelProvider) protected readonly labelProvider: LabelProvider,
@inject(WorkspaceInputDialogProps)
protected readonly props: WorkspaceInputDialogProps,
@inject(LabelProvider) protected readonly labelProvider: LabelProvider
) {
super(props, labelProvider);
this.appendCloseButton('Cancel');
@@ -35,5 +38,4 @@ export class WorkspaceInputDialog extends TheiaWorkspaceInputDialog {
this.errorMessageNode.innerText = DialogError.getMessage(error);
}
}
}

View File

@@ -9,14 +9,17 @@ import { FocusTracker, Widget } from '@theia/core/lib/browser';
import { FrontendApplicationStateService } from '@theia/core/lib/browser/frontend-application-state';
import { WorkspaceService as TheiaWorkspaceService } from '@theia/workspace/lib/browser/workspace-service';
import { ConfigService } from '../../../common/protocol/config-service';
import { SketchesService, Sketch, SketchContainer } from '../../../common/protocol/sketches-service';
import {
SketchesService,
Sketch,
SketchContainer,
} from '../../../common/protocol/sketches-service';
import { ArduinoWorkspaceRootResolver } from '../../arduino-workspace-resolver';
import { BoardsServiceProvider } from '../../boards/boards-service-provider';
import { BoardsConfig } from '../../boards/boards-config';
@injectable()
export class WorkspaceService extends TheiaWorkspaceService {
@inject(SketchesService)
protected readonly sketchService: SketchesService;
@@ -46,8 +49,12 @@ export class WorkspaceService extends TheiaWorkspaceService {
this.application = application;
const info = await this.applicationServer.getApplicationInfo();
this.version = info?.version;
application.shell.onDidChangeCurrentWidget(this.onCurrentWidgetChange.bind(this));
const newValue = application.shell.currentWidget ? application.shell.currentWidget : null;
application.shell.onDidChangeCurrentWidget(
this.onCurrentWidgetChange.bind(this)
);
const newValue = application.shell.currentWidget
? application.shell.currentWidget
: null;
this.onCurrentWidgetChange({ newValue, oldValue: null });
}
@@ -61,10 +68,14 @@ export class WorkspaceService extends TheiaWorkspaceService {
const hash = window.location.hash;
const [recentWorkspaces, recentSketches] = await Promise.all([
this.server.getRecentWorkspaces(),
this.sketchService.getSketches({}).then(container => SketchContainer.toArray(container).map(s => s.uri))
this.sketchService
.getSketches({})
.then((container) =>
SketchContainer.toArray(container).map((s) => s.uri)
),
]);
const toOpen = await new ArduinoWorkspaceRootResolver({
isValid: this.isValid.bind(this)
isValid: this.isValid.bind(this),
}).resolve({ hash, recentWorkspaces, recentSketches });
if (toOpen) {
const { uri } = toOpen;
@@ -73,12 +84,17 @@ export class WorkspaceService extends TheiaWorkspaceService {
}
return (await this.sketchService.createNewSketch()).uri;
} catch (err) {
this.appStateService.reachedState('ready').then(() => this.application.shell.update());
this.logger.fatal(`Failed to determine the sketch directory: ${err}`)
this.appStateService
.reachedState('ready')
.then(() => this.application.shell.update());
this.logger.fatal(
`Failed to determine the sketch directory: ${err}`
);
this.messageService.error(
'There was an error creating the sketch directory. ' +
'See the log for more details. ' +
'The application will probably not work as expected.');
'See the log for more details. ' +
'The application will probably not work as expected.'
);
return super.getDefaultWorkspaceUri();
}
})();
@@ -87,7 +103,10 @@ export class WorkspaceService extends TheiaWorkspaceService {
protected openNewWindow(workspacePath: string): void {
const { boardsConfig } = this.boardsServiceProvider;
const url = BoardsConfig.Config.setConfig(boardsConfig, new URL(window.location.href)); // Set the current boards config for the new browser window.
const url = BoardsConfig.Config.setConfig(
boardsConfig,
new URL(window.location.href)
); // Set the current boards config for the new browser window.
url.hash = workspacePath;
this.windowService.openNewWindow(url.toString());
}
@@ -100,7 +119,9 @@ export class WorkspaceService extends TheiaWorkspaceService {
return this.sketchService.isSketchFolder(uri);
}
protected onCurrentWidgetChange({ newValue }: FocusTracker.IChangedArgs<Widget>): void {
protected onCurrentWidgetChange({
newValue,
}: FocusTracker.IChangedArgs<Widget>): void {
if (newValue instanceof EditorWidget) {
const { uri } = newValue.editor;
if (Sketch.isSketchFile(uri.toString())) {
@@ -108,7 +129,9 @@ export class WorkspaceService extends TheiaWorkspaceService {
} else {
const title = this.workspaceTitle;
const fileName = this.labelProvider.getName(uri);
document.title = this.formatTitle(title ? `${title} - ${fileName}` : fileName);
document.title = this.formatTitle(
title ? `${title} - ${fileName}` : fileName
);
}
} else {
this.updateTitle();
@@ -126,5 +149,4 @@ export class WorkspaceService extends TheiaWorkspaceService {
return this.labelProvider.getName(this.workspace.resource);
}
}
}

View File

@@ -6,7 +6,6 @@ import { SketchesServiceClientImpl } from '../../../common/protocol/sketches-ser
@injectable()
export class WorkspaceVariableContribution extends TheiaWorkspaceVariableContribution {
@inject(SketchesServiceClientImpl)
protected readonly sketchesServiceClient: SketchesServiceClientImpl;
@@ -14,7 +13,10 @@ export class WorkspaceVariableContribution extends TheiaWorkspaceVariableContrib
@postConstruct()
protected init(): void {
this.sketchesServiceClient.currentSketch().then().then(sketch => this.currentSketch = sketch);
this.sketchesServiceClient
.currentSketch()
.then()
.then((sketch) => (this.currentSketch = sketch));
}
getResourceUri(): URI | undefined {