-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #5543: implemented "show all opened terminals" quick-open command
- Implemented the "Show All Opened Terminals" command in the quick command menu, which can display all the currently opened terminal widgets. - User can navigate to each terminal widget by selection, and has the option to create a new terminal. - The command has a global prefix: `term `, to be aligned to VS Code. Signed-off-by: fangnx <naxin.fang@ericsson.com>
- Loading branch information
Showing
3 changed files
with
191 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
packages/terminal/src/browser/terminal-quick-open-service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2019 Ericsson and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import { inject, injectable } from 'inversify'; | ||
import { | ||
QuickOpenModel, QuickOpenGroupItem, QuickOpenHandler, | ||
QuickOpenOptions, QuickOpenItemOptions, QuickOpenMode, | ||
PrefixQuickOpenService, | ||
QuickOpenContribution, QuickOpenHandlerRegistry, QuickOpenGroupItemOptions | ||
} from '@theia/core/lib/browser'; | ||
import { CommandContribution } from '@theia/core/lib/common'; | ||
import { CommandRegistry } from '@theia/core/lib/common'; | ||
import { TerminalWidget } from './base/terminal-widget'; | ||
import { TerminalService } from './base/terminal-service'; | ||
import { TerminalCommands } from './terminal-frontend-contribution'; | ||
|
||
@injectable() | ||
export class TerminalQuickOpenService implements QuickOpenModel, QuickOpenHandler { | ||
|
||
@inject(PrefixQuickOpenService) | ||
protected readonly prefixQuickOpenService: PrefixQuickOpenService; | ||
|
||
@inject(CommandRegistry) | ||
protected readonly commandRegistry: CommandRegistry; | ||
|
||
@inject(TerminalService) | ||
protected readonly terminalService: TerminalService; | ||
|
||
readonly prefix: string = 'term '; | ||
|
||
get description(): string { | ||
return 'Show All Opened Terminals'; | ||
} | ||
|
||
getModel(): QuickOpenModel { | ||
return this; | ||
} | ||
|
||
getOptions(): QuickOpenOptions { | ||
return { | ||
fuzzyMatchLabel: { | ||
enableSeparateSubstringMatching: true | ||
}, | ||
fuzzyMatchDescription: { | ||
enableSeparateSubstringMatching: true | ||
} | ||
}; | ||
} | ||
|
||
open(): void { | ||
this.prefixQuickOpenService.open(this.prefix); | ||
} | ||
|
||
async onType(lookFor: string, acceptor: (items: QuickOpenGroupItem[]) => void): Promise<void> { | ||
const terminalItems: QuickOpenGroupItem[] = []; | ||
|
||
// Get the sorted list of currently opened terminal widgets | ||
const widgets: TerminalWidget[] = this.terminalService.all | ||
.sort((a: TerminalWidget, b: TerminalWidget) => this.compareItems(a, b)); | ||
|
||
for (const widget of widgets) { | ||
const item = await this.toItem(widget); | ||
terminalItems.push(item); | ||
} | ||
// Append a quick open item to create a new terminal. | ||
const createNewTerminalItem = new QuickOpenGroupItem<QuickOpenGroupItemOptions>({ | ||
label: 'Open New Terminal', | ||
iconClass: 'fa fa-plus', | ||
run: this.doCreateNewTerminal(), | ||
groupLabel: undefined, | ||
showBorder: !!terminalItems.length | ||
}); | ||
terminalItems.push(createNewTerminalItem); | ||
|
||
acceptor(terminalItems); | ||
return; | ||
} | ||
|
||
/** | ||
* Compare two terminal widgets by label. If labels are identical, compare by the widget id. | ||
* @param a `TerminalWidget` for comparison | ||
* @param b `TerminalWidget` for comparison | ||
*/ | ||
protected compareItems(a: TerminalWidget, b: TerminalWidget): number { | ||
const normalizedLabel = (str: string) => str.trim().toLowerCase(); | ||
|
||
if (normalizedLabel(a.title.label) !== normalizedLabel(b.title.label)) { | ||
return normalizedLabel(a.title.label).localeCompare(normalizedLabel(b.title.label)); | ||
} else { | ||
return normalizedLabel(a.id).localeCompare(normalizedLabel(b.id)); | ||
} | ||
} | ||
|
||
/** | ||
* Get the function that can create a new terminal. | ||
* @param {TerminalWidget} widget - the terminal widget to be opened. | ||
* @returns Function that would create a new terminal if mode === QuickOpenMode.OPEN. | ||
*/ | ||
protected doCreateNewTerminal(): (mode: QuickOpenMode) => boolean { | ||
return (mode: QuickOpenMode) => { | ||
if (mode !== QuickOpenMode.OPEN) { | ||
return false; | ||
} | ||
this.commandRegistry.executeCommand(TerminalCommands.NEW.id); | ||
return true; | ||
}; | ||
} | ||
|
||
/** | ||
* Convert the terminal widget to the quick open item. | ||
* @param {TerminalWidget} widget - the terminal widget. | ||
* @returns The quick open group item. | ||
*/ | ||
protected async toItem(widget: TerminalWidget): Promise<QuickOpenGroupItem<QuickOpenItemOptions>> { | ||
const options: QuickOpenGroupItemOptions = { | ||
label: widget.title.label, | ||
description: widget.id, | ||
tooltip: widget.title.label, | ||
hidden: false, | ||
run: this.getRunFunction(widget), | ||
groupLabel: undefined, | ||
showBorder: false | ||
}; | ||
return new QuickOpenGroupItem<QuickOpenGroupItemOptions>(options); | ||
} | ||
|
||
/** | ||
* Get the function that can open the editor file. | ||
* @param {TerminalWidget} widget - the terminal widget to be opened. | ||
* @returns Function that would open the terminal if mode === QuickOpenMode.OPEN. | ||
*/ | ||
protected getRunFunction(widget: TerminalWidget): (mode: QuickOpenMode) => boolean { | ||
return (mode: QuickOpenMode) => { | ||
if (mode !== QuickOpenMode.OPEN) { | ||
return false; | ||
} | ||
this.terminalService.open(widget); | ||
return true; | ||
}; | ||
} | ||
} | ||
|
||
/** | ||
* TODO: merge it to TerminalFrontendContribution | ||
*/ | ||
@injectable() | ||
export class TerminalQuickOpenContribution implements CommandContribution, QuickOpenContribution { | ||
|
||
@inject(TerminalQuickOpenService) | ||
protected readonly terminalQuickOpenService: TerminalQuickOpenService; | ||
|
||
registerQuickOpenHandlers(handlers: QuickOpenHandlerRegistry): void { | ||
handlers.registerHandler(this.terminalQuickOpenService); | ||
} | ||
|
||
registerCommands(commands: CommandRegistry): void { | ||
commands.registerCommand(TerminalCommands.SHOW_ALL_OPENED_TERMINALS, { | ||
execute: () => this.terminalQuickOpenService.open() | ||
}); | ||
} | ||
} |