-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Menu to select default formatter for the document #8446
Merged
vitaliy-guliy
merged 1 commit into
eclipse-theia:master
from
vitaliy-guliy:default-formatter-8354
Sep 15, 2020
+233
−15
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
149 changes: 149 additions & 0 deletions
149
packages/monaco/src/browser/monaco-formatting-conflicts.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,149 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2020 Red Hat, Inc. 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 { injectable, inject } from 'inversify'; | ||
import { MonacoQuickOpenService } from './monaco-quick-open-service'; | ||
import { QuickOpenModel, QuickOpenItem, QuickOpenMode } from '@theia/core/lib/common/quick-open-model'; | ||
import { Deferred } from '@theia/core/lib/common/promise-util'; | ||
import { PreferenceService, FrontendApplicationContribution } from '@theia/core/lib/browser'; | ||
import { PreferenceSchemaProvider } from '@theia/core/lib/browser'; | ||
import { EditorManager } from '@theia/editor/lib/browser'; | ||
|
||
type FormattingEditProvider = monaco.languages.DocumentFormattingEditProvider | monaco.languages.DocumentRangeFormattingEditProvider; | ||
|
||
const PREFERENCE_NAME = 'editor.defaultFormatter'; | ||
|
||
@injectable() | ||
export class MonacoFormattingConflictsContribution implements FrontendApplicationContribution { | ||
|
||
@inject(MonacoQuickOpenService) | ||
protected readonly quickOpenService: MonacoQuickOpenService; | ||
|
||
@inject(PreferenceService) | ||
protected readonly preferenceService: PreferenceService; | ||
|
||
@inject(PreferenceSchemaProvider) | ||
protected readonly preferenceSchema: PreferenceSchemaProvider; | ||
|
||
@inject(EditorManager) | ||
protected readonly editorManager: EditorManager; | ||
|
||
async initialize(): Promise<void> { | ||
monaco.format.FormattingConflicts.setFormatterSelector(<T extends FormattingEditProvider>( | ||
formatters: T[], document: monaco.editor.ITextModel, mode: monaco.format.FormattingMode) => | ||
this.selectFormatter(formatters, document, mode)); | ||
} | ||
|
||
private async setDefaultFormatter(language: string, formatter: string): Promise<void> { | ||
const name = this.preferenceSchema.overridePreferenceName({ | ||
preferenceName: PREFERENCE_NAME, | ||
overrideIdentifier: language | ||
}); | ||
|
||
await this.preferenceService.set(name, formatter); | ||
} | ||
|
||
private getDefaultFormatter(language: string): string | undefined { | ||
const name = this.preferenceSchema.overridePreferenceName({ | ||
preferenceName: PREFERENCE_NAME, | ||
overrideIdentifier: language | ||
}); | ||
|
||
return this.preferenceService.get<string>(name); | ||
} | ||
|
||
private async selectFormatter<T extends FormattingEditProvider>( | ||
formatters: T[], document: monaco.editor.ITextModel, mode: monaco.format.FormattingMode): Promise<T | undefined> { | ||
|
||
if (formatters.length === 0) { | ||
return undefined; | ||
} | ||
|
||
if (formatters.length === 1) { | ||
return formatters[0]; | ||
} | ||
|
||
const currentEditor = this.editorManager.currentEditor; | ||
if (!currentEditor) { | ||
return undefined; | ||
} | ||
|
||
const languageId = currentEditor.editor.document.languageId; | ||
const defaultFormatterId = await this.getDefaultFormatter(languageId); | ||
|
||
if (defaultFormatterId) { | ||
const formatter = formatters.find(f => f.extensionId && f.extensionId.value === defaultFormatterId); | ||
if (formatter) { | ||
return formatter; | ||
} | ||
} | ||
|
||
let deferred: Deferred<T> | undefined = new Deferred<T>(); | ||
|
||
const items: QuickOpenItem[] = formatters | ||
.filter(formatter => formatter.displayName) | ||
.map<QuickOpenItem>(formatter => { | ||
const displayName: string = formatter.displayName!; | ||
const extensionId = formatter.extensionId ? formatter.extensionId.value : undefined; | ||
|
||
return new QuickOpenItem({ | ||
label: displayName, | ||
detail: extensionId, | ||
run: (openMode: QuickOpenMode) => { | ||
if (openMode === QuickOpenMode.OPEN) { | ||
if (deferred) { | ||
deferred.resolve(formatter); | ||
deferred = undefined; | ||
} | ||
|
||
this.quickOpenService.hide(); | ||
|
||
this.setDefaultFormatter(languageId, extensionId ? extensionId : ''); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
}); | ||
}) | ||
.sort((a, b) => a.getLabel()!.localeCompare(b.getLabel()!)); | ||
|
||
const model: QuickOpenModel = { | ||
onType(lookFor: string, acceptor: (items: QuickOpenItem[]) => void): void { | ||
acceptor(items); | ||
} | ||
}; | ||
|
||
this.quickOpenService.open(model, | ||
{ | ||
fuzzyMatchDescription: true, | ||
fuzzyMatchLabel: true, | ||
fuzzyMatchDetail: true, | ||
placeholder: 'Select formatter for the current document', | ||
ignoreFocusOut: false, | ||
|
||
onClose: () => { | ||
if (deferred) { | ||
deferred.resolve(undefined); | ||
deferred = undefined; | ||
} | ||
} | ||
}); | ||
|
||
return deferred.promise; | ||
} | ||
|
||
} |
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
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why renaming these is needed?
Why not keep it in sync with the corresponding VS Code interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aligned with VS Code, thanks!