11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT License.
33
4- import { ConfigurationTarget , extensions , WorkspaceConfiguration } from 'vscode' ;
54import { LanguageClientOptions } from 'vscode-languageclient' ;
6- import * as semver from 'semver' ;
75import { IWorkspaceService } from '../../common/application/types' ;
8- import { PYLANCE_EXTENSION_ID } from '../../common/constants' ;
9- import { IExperimentService } from '../../common/types' ;
106
117import { LanguageServerAnalysisOptionsBase } from '../common/analysisOptions' ;
128import { ILanguageServerOutputChannel } from '../types' ;
13- import { traceWarn } from '../../logging' ;
14-
15- const EDITOR_CONFIG_SECTION = 'editor' ;
16- const FORMAT_ON_TYPE_CONFIG_SETTING = 'formatOnType' ;
179
1810export class NodeLanguageServerAnalysisOptions extends LanguageServerAnalysisOptionsBase {
1911 // eslint-disable-next-line @typescript-eslint/no-useless-constructor
20- constructor (
21- lsOutputChannel : ILanguageServerOutputChannel ,
22- workspace : IWorkspaceService ,
23- private readonly experimentService : IExperimentService ,
24- ) {
12+ constructor ( lsOutputChannel : ILanguageServerOutputChannel , workspace : IWorkspaceService ) {
2513 super ( lsOutputChannel , workspace ) ;
2614 }
2715
@@ -34,72 +22,6 @@ export class NodeLanguageServerAnalysisOptions extends LanguageServerAnalysisOpt
3422 return ( {
3523 experimentationSupport : true ,
3624 trustedWorkspaceSupport : true ,
37- autoIndentSupport : await this . isAutoIndentEnabled ( ) ,
3825 } as unknown ) as LanguageClientOptions ;
3926 }
40-
41- private async isAutoIndentEnabled ( ) {
42- let editorConfig = this . getPythonSpecificEditorSection ( ) ;
43-
44- // Only explicitly enable formatOnType for those who are in the experiment
45- // but have not explicitly given a value for the setting
46- if ( ! NodeLanguageServerAnalysisOptions . isConfigSettingSetByUser ( editorConfig , FORMAT_ON_TYPE_CONFIG_SETTING ) ) {
47- const inExperiment = await this . isInAutoIndentExperiment ( ) ;
48- if ( inExperiment ) {
49- await NodeLanguageServerAnalysisOptions . setPythonSpecificFormatOnType ( editorConfig , true ) ;
50-
51- // Refresh our view of the config settings.
52- editorConfig = this . getPythonSpecificEditorSection ( ) ;
53- }
54- }
55-
56- const formatOnTypeEffectiveValue = editorConfig . get ( FORMAT_ON_TYPE_CONFIG_SETTING ) ;
57-
58- return formatOnTypeEffectiveValue ;
59- }
60-
61- private static isConfigSettingSetByUser ( configuration : WorkspaceConfiguration , setting : string ) : boolean {
62- const inspect = configuration . inspect ( setting ) ;
63- if ( inspect === undefined ) {
64- return false ;
65- }
66-
67- return (
68- inspect . globalValue !== undefined ||
69- inspect . workspaceValue !== undefined ||
70- inspect . workspaceFolderValue !== undefined ||
71- inspect . globalLanguageValue !== undefined ||
72- inspect . workspaceLanguageValue !== undefined ||
73- inspect . workspaceFolderLanguageValue !== undefined
74- ) ;
75- }
76-
77- private async isInAutoIndentExperiment ( ) : Promise < boolean > {
78- if ( await this . experimentService . inExperiment ( 'pylanceAutoIndent' ) ) {
79- return true ;
80- }
81-
82- const pylanceVersion = extensions . getExtension ( PYLANCE_EXTENSION_ID ) ?. packageJSON . version as string ;
83- return pylanceVersion !== undefined && semver . prerelease ( pylanceVersion ) ?. includes ( 'dev' ) === true ;
84- }
85-
86- private getPythonSpecificEditorSection ( ) {
87- return this . workspace . getConfiguration ( EDITOR_CONFIG_SECTION , undefined , /* languageSpecific */ true ) ;
88- }
89-
90- private static async setPythonSpecificFormatOnType (
91- editorConfig : WorkspaceConfiguration ,
92- value : boolean | undefined ,
93- ) {
94- try {
95- await editorConfig . update (
96- FORMAT_ON_TYPE_CONFIG_SETTING ,
97- value ,
98- ConfigurationTarget . Global ,
99- /* overrideInLanguage */ true ,
100- ) ;
101- } catch ( ex ) {
102- traceWarn ( `Failed to set formatOnType to ${ value } ` ) ;
103- }
104- }
10527}
0 commit comments