1+ /**
2+ * Copyright 2022 Red Hat, Inc. and others.
3+
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ import { commands , ConfigurationTarget , Disposable , workspace , window } from 'vscode' ;
18+ import { getDirectoryPath , getRelativePath , getWorkspaceUri } from './utils/fileUtils' ;
19+ import * as CommandKind from './commands/lspCommandConstants' ;
20+
21+ /**
22+ * Registers the `CommandKind.UPDATE_CONFIGURATION` command
23+ */
24+ export function registerConfigurationUpdateCommand ( ) : Disposable {
25+ return commands . registerCommand ( CommandKind . UPDATE_CONFIGURATION , resolveConfigurationItemEdit ) ;
26+ }
27+
28+ function resolveConfigurationItemEdit < T > ( configurationItemEdit : ConfigurationItemEdit ) {
29+ if ( configurationItemEdit . valueKind == ConfigurationItemValueKind . file ) {
30+ configurationItemEdit . value = resolveFilePath ( configurationItemEdit . value ) ;
31+ }
32+ switch ( configurationItemEdit . editType ) {
33+ case ConfigurationItemEditType . Add :
34+ addToPreferenceArray < T > ( configurationItemEdit . section , configurationItemEdit . value ) ;
35+ break ;
36+ case ConfigurationItemEditType . Delete : {
37+ removeFromPreferenceArray < T > ( configurationItemEdit . section , configurationItemEdit . value ) ;
38+ }
39+ }
40+ }
41+
42+ function resolveFilePath ( filePath : any ) : any {
43+ const currentWorkspaceUri = getWorkspaceUri ( window . activeTextEditor . document ) . toString ( ) ;
44+ return getDirectoryPath ( filePath ) . includes ( currentWorkspaceUri ) ? getRelativePath ( currentWorkspaceUri , filePath ) : filePath ;
45+
46+ }
47+
48+ function addToPreferenceArray < T > ( key : string , value : T ) : void {
49+ const configArray : T [ ] = workspace . getConfiguration ( ) . get < T [ ] > ( key , [ ] ) ;
50+ if ( configArray . includes ( value ) ) {
51+ return ;
52+ }
53+ configArray . push ( value ) ;
54+ workspace . getConfiguration ( ) . update ( key , configArray , ConfigurationTarget . Workspace ) ;
55+ }
56+
57+ function removeFromPreferenceArray < T > ( key : string , value : T ) : void {
58+ const configArray : T [ ] = workspace . getConfiguration ( ) . get < T [ ] > ( key , [ ] ) ;
59+ if ( ! configArray . includes ( value ) ) {
60+ return ;
61+ }
62+ const resultPref = configArray . filter ( i => i != value ) ;
63+ workspace . getConfiguration ( ) . update ( key , resultPref , ConfigurationTarget . Workspace ) ;
64+ }
65+
66+ interface ConfigurationItemEdit {
67+ section : string ;
68+ value : any ;
69+ editType : ConfigurationItemEditType ;
70+ valueKind : ConfigurationItemValueKind ;
71+ }
72+
73+ enum ConfigurationItemEditType {
74+ Add = 0 ,
75+ Delete = 1
76+ }
77+
78+ enum ConfigurationItemValueKind {
79+ file = 0
80+ }
0 commit comments