From ca17c937c1c470ecaf022c685e8f8b14f55ab455 Mon Sep 17 00:00:00 2001 From: 9brada6 <9brada6@gmail.com> Date: Sun, 19 Apr 2020 10:03:00 +0300 Subject: [PATCH 1/2] Added the feature to auto-detect first workspace directory and set it as default analyzedProjectDirectory. analyzedProjectDirectory is no longer needed to be explicit set. --- src/extension.ts | 61 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 14 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index f9f4fed..f298b03 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -163,17 +163,12 @@ async function checkPhanSupportsLanguageServer(context: vscode.ExtensionContext, // Returns true if phan.phanScriptPath supports the language server protocol. async function checkValidAnalyzedProjectDirectory(context: vscode.ExtensionContext, analyzedProjectDirectory: string): Promise { - const exists: boolean = isDirectory(analyzedProjectDirectory); - - if (!exists) { + if (!isDirectory(analyzedProjectDirectory)) { await showOpenSettingsPrompt('The setting phan.analyzedProjectDirectory refers to a directory that does not exist. directory: ' + analyzedProjectDirectory); return false; } - const phanConfigPath = path.join(analyzedProjectDirectory, '.phan', 'config.php'); - const phanConfigExists: boolean = isFile(phanConfigPath); - - if (!phanConfigExists) { + if (!pathContainsPhanFolderAndConfig(analyzedProjectDirectory)) { await showOpenSettingsPrompt('The setting phan.analyzedProjectDirectory refers to a directory that does not contain .phan/config.php. Check that it is the absolute path of the root of a project set up for phan. directory: ' + analyzedProjectDirectory); return false; } @@ -203,7 +198,7 @@ export async function activate(context: vscode.ExtensionContext): Promise const phanScriptPath = conf.get('phanScriptPath') || defaultPhanScriptPath; // Support analyzing more than one project the simplest way (always start every server). const originalAnalyzedProjectDirectories = conf.get('analyzedProjectDirectory'); - const analyzedProjectDirectories = normalizeDirsToAnalyze(originalAnalyzedProjectDirectories); + let analyzedProjectDirectories = normalizeDirsToAnalyze(originalAnalyzedProjectDirectories); const enableDebugLog = conf.get('enableDebugLog'); const analyzeOnlyOnSave = conf.get('analyzeOnlyOnSave'); const allowPolyfillParser = conf.get('allowPolyfillParser') || false; @@ -255,13 +250,16 @@ export async function activate(context: vscode.ExtensionContext): Promise const phanScriptPathValidated = phanScriptPath; // work around typescript complaint. // Check if the analyzedProjectDirectories setting was provided. - if (!analyzedProjectDirectories) { - if (originalAnalyzedProjectDirectories instanceof Array) { - await showOpenSettingsPrompt('The setting phan.analyzedProjectDirectories must be a path or a non-empty array of paths (e.g. /path/to/some/project_folder). `.phan` must be a folder within that directory.'); - } else { - await showOpenSettingsPrompt('The setting phan.analyzedProjectDirectories must be provided (e.g. /path/to/some/project_folder). `.phan` must be a folder within that directory.'); + if (!analyzedProjectDirectories.length) { + analyzedProjectDirectories = findValidProjectDirectories(); + + if (!analyzedProjectDirectories.length) { + const cantFindWorkspaceDirectoryMessage = + 'No workspace directory contain a folder named ".phan" with a config.php file. ' + + 'You can add custom directories via phan.analyzedProjectDirectories setting.'; + await showOpenSettingsPrompt(cantFindWorkspaceDirectoryMessage); + return; } - return; } for (const dir of analyzedProjectDirectories) { @@ -424,3 +422,38 @@ export async function activate(context: vscode.ExtensionContext): Promise context.subscriptions.push(createClient(dirToAnalyze).start()); } } + +// Search all the workspace folders and return the first that contains .phan/config.php +// This will return the folder name in an array as required, and just only one folder, +// the first met. We can easy modify the function to add all valid workspace folders to the array. +function findValidProjectDirectories(): string[] { + // Get the fsPath(file system path) of all workspace folders. + const VSCodeFolders = vscode.workspace.workspaceFolders; + + if (!VSCodeFolders) { + return []; + } + + let workingFolders = VSCodeFolders.map( function(obj) { + if ( ('uri' in obj) && ('fsPath' in obj.uri)) { + return obj.uri.fsPath; + } + return ''; + } ); + + // Return the first workspace folder that satisfy our criteria. + for (let i = 0; i < workingFolders.length; i++) { + if ( pathContainsPhanFolderAndConfig( workingFolders[i] ) ) { + return [ workingFolders[i] ]; + } + } + + return []; +} + +// Whether or not a path contains the ".phan" folder alongside with a config.php file. +// Returns bool +function pathContainsPhanFolderAndConfig(folderPath: string): boolean { + let phanConfigPath = path.join(folderPath, '.phan', 'config.php'); + return isFile(phanConfigPath); +} From cc63d302a3a0ab72d897d602a79662cdc831014f Mon Sep 17 00:00:00 2001 From: 9brada6 <9brada6@gmail.com> Date: Sun, 19 Apr 2020 11:17:01 +0300 Subject: [PATCH 2/2] Make phan auto-detect all project derectories from workspace folders, and do not show alerting errors if no directory was found. --- src/extension.ts | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index f298b03..0b863a3 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -254,10 +254,11 @@ export async function activate(context: vscode.ExtensionContext): Promise analyzedProjectDirectories = findValidProjectDirectories(); if (!analyzedProjectDirectories.length) { + // Do not send an error to the interface, this is frustrating. const cantFindWorkspaceDirectoryMessage = 'No workspace directory contain a folder named ".phan" with a config.php file. ' + 'You can add custom directories via phan.analyzedProjectDirectories setting.'; - await showOpenSettingsPrompt(cantFindWorkspaceDirectoryMessage); + console.warn(cantFindWorkspaceDirectoryMessage); return; } } @@ -429,26 +430,21 @@ export async function activate(context: vscode.ExtensionContext): Promise function findValidProjectDirectories(): string[] { // Get the fsPath(file system path) of all workspace folders. const VSCodeFolders = vscode.workspace.workspaceFolders; - if (!VSCodeFolders) { return []; } - - let workingFolders = VSCodeFolders.map( function(obj) { - if ( ('uri' in obj) && ('fsPath' in obj.uri)) { + let workingFolders = VSCodeFolders.map(function (obj) { + if (('uri' in obj) && ('fsPath' in obj.uri)) { return obj.uri.fsPath; } return ''; - } ); + }); - // Return the first workspace folder that satisfy our criteria. - for (let i = 0; i < workingFolders.length; i++) { - if ( pathContainsPhanFolderAndConfig( workingFolders[i] ) ) { - return [ workingFolders[i] ]; - } - } + workingFolders = workingFolders.filter(function (folderPath) { + return pathContainsPhanFolderAndConfig(folderPath); + }); - return []; + return workingFolders; } // Whether or not a path contains the ".phan" folder alongside with a config.php file.