44 *--------------------------------------------------------------------------------------------*/
55'use strict' ;
66
7- import * as fs from 'fs' ;
8- import * as path from 'path' ;
9-
107import PHPCompletionItemProvider from './features/completionItemProvider' ;
118import PHPHoverProvider from './features/hoverProvider' ;
129import PHPSignatureHelpProvider from './features/signatureHelpProvider' ;
@@ -15,63 +12,11 @@ import * as vscode from 'vscode';
1512
1613import * as nls from 'vscode-nls' ;
1714nls . config ( { locale : vscode . env . language } ) ;
18- let localize = nls . loadMessageBundle ( ) ;
19-
20- const MigratedKey = 'php.validate.executablePaht.migrated' ;
21- const PathKey = 'php.validate.executablePath' ;
22-
23- namespace is {
24- const toString = Object . prototype . toString ;
25-
26- export function string ( value : any ) : value is string {
27- return toString . call ( value ) === '[object String]' ;
28- }
29- }
30-
31- let statusBarItem : vscode . StatusBarItem ;
3215
3316export function activate ( context : vscode . ExtensionContext ) : any {
3417
35- let workspaceExecutablePath = context . workspaceState . get < string > ( PathKey , undefined ) ;
36- let migrated = context . workspaceState . get < boolean > ( MigratedKey , false ) ;
37- let validator = new PHPValidationProvider ( workspaceExecutablePath ) ;
38- context . subscriptions . push ( vscode . commands . registerCommand ( '_php.onPathClicked' , ( ) => {
39- onPathClicked ( context , validator ) ;
40- } ) ) ;
41-
42- statusBarItem = vscode . window . createStatusBarItem ( vscode . StatusBarAlignment . Right , Number . MIN_VALUE ) ;
43- statusBarItem . text = localize ( 'php.path' , 'Path' ) ;
44- statusBarItem . color = 'white' ;
45- statusBarItem . command = '_php.onPathClicked' ;
46- vscode . workspace . onDidChangeConfiguration ( ( ) => updateStatusBarItem ( context ) ) ;
47- vscode . window . onDidChangeActiveTextEditor ( ( editor ) => {
48- updateStatusBarItem ( context , editor ) ;
49- } ) ;
50- updateStatusBarItem ( context , vscode . window . activeTextEditor ) ;
51-
52- if ( workspaceExecutablePath === void 0 && ! migrated ) {
53- let settingsExecutablePath = readLocalExecutableSetting ( ) ;
54- if ( settingsExecutablePath ) {
55- migrateExecutablePath ( settingsExecutablePath ) . then ( ( value ) => {
56- context . workspaceState . update ( MigratedKey , true ) ;
57- // User has pressed escape;
58- if ( ! value ) {
59- // activate the validator with the current settings.
60- validator . activate ( context . subscriptions ) ;
61- return ;
62- }
63- context . workspaceState . update ( PathKey , value ) ;
64- validator . updateWorkspaceExecutablePath ( value , false ) ;
65- validator . activate ( context . subscriptions ) ;
66- updateStatusBarItem ( context ) ;
67- } ) ;
68- } else {
69- context . workspaceState . update ( MigratedKey , true ) ;
70- validator . activate ( context . subscriptions ) ;
71- }
72- } else {
73- validator . activate ( context . subscriptions ) ;
74- }
18+ let validator = new PHPValidationProvider ( context . workspaceState ) ;
19+ validator . activate ( context . subscriptions ) ;
7520
7621 // add providers
7722 context . subscriptions . push ( vscode . languages . registerCompletionItemProvider ( 'php' , new PHPCompletionItemProvider ( ) , '.' , '$' ) ) ;
@@ -83,126 +28,4 @@ export function activate(context: vscode.ExtensionContext): any {
8328 vscode . languages . setLanguageConfiguration ( 'php' , {
8429 wordPattern : / ( - ? \d * \. \d \w * ) | ( [ ^ \- \` \~ \! \@ \# \% \^ \& \* \( \) \= \+ \[ \{ \] \} \\ \| \; \: \' \" \, \. \< \> \/ \? \s ] + ) / g
8530 } ) ;
86- }
87-
88- function updateStatusBarItem ( context : vscode . ExtensionContext , editor : vscode . TextEditor = vscode . window . activeTextEditor ) : void {
89- statusBarItem . tooltip = getExecutablePath ( context ) ;
90- if ( editor && editor . document && editor . document . languageId === 'php' ) {
91- statusBarItem . show ( ) ;
92- } else {
93- statusBarItem . hide ( ) ;
94- }
95- }
96-
97- function onPathClicked ( context : vscode . ExtensionContext , validator : PHPValidationProvider ) {
98- let value = getExecutablePath ( context ) ;
99- vscode . window . showInputBox ( { prompt : localize ( 'php.enterPath' , 'The path to the PHP executable' ) , value : value || '' } ) . then ( value => {
100- if ( ! value ) {
101- // User pressed Escape
102- return ;
103- }
104- context . workspaceState . update ( PathKey , value ) ;
105- validator . updateWorkspaceExecutablePath ( value , true ) ;
106- updateStatusBarItem ( context ) ;
107- } , ( error ) => {
108- } ) ;
109- }
110-
111- function getExecutablePath ( context : vscode . ExtensionContext ) : string {
112- let result = context . workspaceState . get < string > ( PathKey , undefined ) ;
113- if ( result ) {
114- return result ;
115- }
116- let section = vscode . workspace . getConfiguration ( 'php.validate' ) ;
117- if ( section ) {
118- return section . get ( 'executablePath' , undefined ) ;
119- }
120- return undefined ;
121- }
122-
123- function migrateExecutablePath ( settingsExecutablePath : string ) : Thenable < string > {
124- return vscode . window . showInformationMessage (
125- localize ( 'php.migrateWorkspaceSetting' , 'Do you want to use {0} as your future PHP executable path?' , settingsExecutablePath ) ,
126- {
127- title : localize ( 'php.yes' , 'Yes' ) ,
128- id : 'yes'
129- } ,
130- {
131- title : localize ( 'php.edit' , 'Edit' ) ,
132- id : 'edit'
133- } ,
134- {
135- title : localize ( 'php.more' , 'Learn More' ) ,
136- id : 'more'
137- }
138- ) . then ( ( selected ) => {
139- if ( ! selected ) {
140- return undefined ;
141- }
142- if ( selected . id === 'yes' ) {
143- return settingsExecutablePath ;
144- } else if ( selected . id === 'edit' ) {
145- return vscode . window . showInputBox (
146- {
147- prompt : localize ( 'php.migrateExecutablePath' , 'Use the above path as the PHP executable path?' ) ,
148- value : settingsExecutablePath
149- }
150- ) ;
151- } else if ( selected . id === 'more' ) {
152- vscode . commands . executeCommand ( 'vscode.open' , vscode . Uri . parse ( 'https://go.microsoft.com/fwlink/?linkid=839919' ) ) ;
153- return undefined ;
154- }
155- } ) ;
156- }
157-
158- function readLocalExecutableSetting ( ) : string {
159- function stripComments ( content : string ) : string {
160- /**
161- * First capturing group matches double quoted string
162- * Second matches single quotes string
163- * Third matches block comments
164- * Fourth matches line comments
165- */
166- var regexp : RegExp = / ( " (?: [ ^ \\ \" ] * (?: \\ .) ? ) * " ) | ( ' (?: [ ^ \\ \' ] * (?: \\ .) ? ) * ' ) | ( \/ \* (?: \r ? \n | .) * ?\* \/ ) | ( \/ { 2 , } .* ?(?: (?: \r ? \n ) | $ ) ) / g;
167- let result = content . replace ( regexp , ( match , m1 , m2 , m3 , m4 ) => {
168- // Only one of m1, m2, m3, m4 matches
169- if ( m3 ) {
170- // A block comment. Replace with nothing
171- return '' ;
172- } else if ( m4 ) {
173- // A line comment. If it ends in \r?\n then keep it.
174- let length = m4 . length ;
175- if ( length > 2 && m4 [ length - 1 ] === '\n' ) {
176- return m4 [ length - 2 ] === '\r' ? '\r\n' : '\n' ;
177- } else {
178- return '' ;
179- }
180- } else {
181- // We match a string
182- return match ;
183- }
184- } ) ;
185- return result ;
186- } ;
187-
188- try {
189- let rootPath = vscode . workspace . rootPath ;
190- if ( ! rootPath ) {
191- return undefined ;
192- }
193- let settingsFile = path . join ( rootPath , '.vscode' , 'settings.json' ) ;
194- if ( ! fs . existsSync ( settingsFile ) ) {
195- return undefined ;
196- }
197- let content = fs . readFileSync ( settingsFile , 'utf8' ) ;
198- if ( ! content || content . length === 0 ) {
199- return undefined ;
200- }
201- content = stripComments ( content ) ;
202- let json = JSON . parse ( content ) ;
203- let value = json [ 'php.validate.executablePath' ] ;
204- return is . string ( value ) ? value : undefined ;
205- } catch ( error ) {
206- }
207- return undefined ;
20831}
0 commit comments