1515// *****************************************************************************
1616
1717import { inject , injectable , postConstruct } from '@theia/core/shared/inversify' ;
18- import { Emitter , Event , PreferenceLanguageOverrideService , PreferenceScope , PreferenceService } from '@theia/core' ;
18+ import { Emitter , Event , PreferenceInspection , PreferenceLanguageOverrideService , PreferenceScope , PreferenceService } from '@theia/core' ;
19+ import { WorkspaceService } from '@theia/workspace/lib/browser' ;
1920import { FormatterInfo , FormatterService , FormatterSettingScope , FormatterStatus } from '@theia/editor/lib/browser/editor-formatter-service' ;
2021import { TextEditor } from '@theia/editor/lib/browser' ;
2122import { StandaloneServices } from '@theia/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneServices' ;
@@ -54,6 +55,9 @@ export class MonacoFormatterService implements FormatterService {
5455 @inject ( PreferenceLanguageOverrideService )
5556 protected readonly preferenceSchema : PreferenceLanguageOverrideService ;
5657
58+ @inject ( WorkspaceService )
59+ protected readonly workspaceService : WorkspaceService ;
60+
5761 protected readonly onDidChangeFormattersEmitter = new Emitter < void > ( ) ;
5862 readonly onDidChangeFormatters : Event < void > = this . onDidChangeFormattersEmitter . event ;
5963
@@ -125,14 +129,8 @@ export class MonacoFormatterService implements FormatterService {
125129 return undefined ;
126130 }
127131
128- let scope : FormatterSettingScope ;
129- if ( inspection . workspaceFolderValue !== undefined ) {
130- scope = 'folder' ;
131- } else if ( inspection . workspaceValue !== undefined ) {
132- scope = 'workspace' ;
133- } else {
134- scope = 'user' ;
135- }
132+ const preferenceScope = this . getConfiguredScopeFromInspection ( inspection ) ;
133+ const scope = this . preferenceToFormatterScope ( preferenceScope ) ;
136134 const formatter = formatters . find ( f => f . id === configuredFormatterId ) ;
137135
138136 return {
@@ -143,6 +141,37 @@ export class MonacoFormatterService implements FormatterService {
143141 } ;
144142 }
145143
144+ /**
145+ * Determines the preference scope from an inspection result.
146+ * In single-folder workspaces, folder and workspace scopes are equivalent,
147+ * so we need to check if we're in a multi-root workspace.
148+ */
149+ protected getConfiguredScopeFromInspection < T > ( inspection : PreferenceInspection < T > ) : PreferenceScope | undefined {
150+ if ( this . workspaceService . isMultiRootWorkspaceOpened && inspection . workspaceFolderValue !== undefined ) {
151+ return PreferenceScope . Folder ;
152+ }
153+ if ( inspection . workspaceValue !== undefined ) {
154+ return PreferenceScope . Workspace ;
155+ }
156+ if ( inspection . globalValue !== undefined ) {
157+ return PreferenceScope . User ;
158+ }
159+ return undefined ;
160+ }
161+
162+ protected preferenceToFormatterScope ( scope : PreferenceScope | undefined ) : FormatterSettingScope {
163+ switch ( scope ) {
164+ case PreferenceScope . Folder :
165+ return 'folder' ;
166+ case PreferenceScope . Workspace :
167+ return 'workspace' ;
168+ case PreferenceScope . User :
169+ return 'user' ;
170+ default :
171+ return 'none' ;
172+ }
173+ }
174+
146175 getAvailableFormatters ( editor : TextEditor ) : FormatterInfo [ ] {
147176 const model = this . getEditorModel ( editor ) ;
148177 if ( ! model ) {
@@ -207,15 +236,6 @@ export class MonacoFormatterService implements FormatterService {
207236 return undefined ;
208237 }
209238
210- if ( inspection . workspaceFolderValue !== undefined ) {
211- return PreferenceScope . Folder ;
212- }
213- if ( inspection . workspaceValue !== undefined ) {
214- return PreferenceScope . Workspace ;
215- }
216- if ( inspection . globalValue !== undefined ) {
217- return PreferenceScope . User ;
218- }
219- return undefined ;
239+ return this . getConfiguredScopeFromInspection ( inspection ) ;
220240 }
221241}
0 commit comments