|
| 1 | +// Copyright 2024 The MathWorks, Inc. |
| 2 | + |
| 3 | +import { WorkspaceFolder, WorkspaceFoldersChangeEvent } from 'vscode-languageserver' |
| 4 | +import ClientConnection, { Connection } from '../ClientConnection' |
| 5 | +import Logger from '../logging/Logger' |
| 6 | +import MatlabLifecycleManager from './MatlabLifecycleManager' |
| 7 | +import { MatlabConnection } from './MatlabCommunicationManager' |
| 8 | +import * as os from 'os' |
| 9 | +import path from 'path' |
| 10 | + |
| 11 | +export default class PathSynchronizer { |
| 12 | + readonly CD_REQUEST_CHANNEL = '/matlabls/pathSynchronizer/cd/request' |
| 13 | + |
| 14 | + readonly PWD_REQUEST_CHANNEL = '/matlabls/pathSynchronizer/pwd/request' |
| 15 | + readonly PWD_RESPONSE_CHANNEL = '/matlabls/pathSynchronizer/pwd/response' |
| 16 | + |
| 17 | + readonly ADDPATH_REQUEST_CHANNEL = '/matlabls/pathSynchronizer/addpath/request' |
| 18 | + readonly RMPATH_REQUEST_CHANNEL = '/matlabls/pathSynchronizer/rmpath/request' |
| 19 | + |
| 20 | + constructor (private readonly matlabLifecycleManager: MatlabLifecycleManager) {} |
| 21 | + |
| 22 | + /** |
| 23 | + * Initializes the PathSynchronizer by setting up event listeners. |
| 24 | + * |
| 25 | + * Upon MATLAB connection, all workspace folders are added to the MATLAB search path. |
| 26 | + * Additionally, MATLAB's CWD is set to the first workspace folder to avoid potential |
| 27 | + * function shadowing issues. |
| 28 | + * |
| 29 | + * As workspace folders are added or removed, the MATLAB path is updated accordingly. |
| 30 | + */ |
| 31 | + initialize () { |
| 32 | + const clientConnection = ClientConnection.getConnection() |
| 33 | + |
| 34 | + this.matlabLifecycleManager.eventEmitter.on('connected', () => this.handleMatlabConnected(clientConnection)) |
| 35 | + |
| 36 | + clientConnection.workspace.onDidChangeWorkspaceFolders(event => this.handleWorkspaceFoldersChanged(event)) |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Handles synchronizing the MATLAB path with the current workspace folders when MATLAB |
| 41 | + * has been connected. |
| 42 | + * |
| 43 | + * @param clientConnection The current client connection |
| 44 | + */ |
| 45 | + private async handleMatlabConnected (clientConnection: Connection): Promise<void> { |
| 46 | + const matlabConnection = await this.matlabLifecycleManager.getMatlabConnection() |
| 47 | + if (matlabConnection == null) { |
| 48 | + // As the connection was just established, this should not happen |
| 49 | + Logger.warn('MATLAB connection is unavailable after connection established') |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + const workspaceFolders = await clientConnection.workspace.getWorkspaceFolders() |
| 54 | + if (workspaceFolders == null || workspaceFolders.length === 0) { |
| 55 | + // No workspace folders - no action needs to be taken |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + const folderPaths = this.convertWorkspaceFoldersToFilePaths(workspaceFolders) |
| 60 | + |
| 61 | + // cd to first workspace folder |
| 62 | + this.setWorkingDirectory(folderPaths[0], matlabConnection) |
| 63 | + |
| 64 | + // add all workspace folders to path |
| 65 | + this.addToPath(folderPaths, matlabConnection) |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Handles synchronizing the MATLAB path with newly added/removed workspace folders. |
| 70 | + * |
| 71 | + * @param event The workspace folders change event |
| 72 | + */ |
| 73 | + private async handleWorkspaceFoldersChanged (event: WorkspaceFoldersChangeEvent): Promise<void> { |
| 74 | + const matlabConnection = await this.matlabLifecycleManager.getMatlabConnection() |
| 75 | + if (matlabConnection == null) { |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + const cwd = await this.getCurrentWorkingDirectory(matlabConnection) |
| 80 | + |
| 81 | + // addpath for all added folders |
| 82 | + const addedFolderPaths = this.convertWorkspaceFoldersToFilePaths(event.added) |
| 83 | + this.addToPath(addedFolderPaths, matlabConnection) |
| 84 | + |
| 85 | + // rmpath for all removed folders |
| 86 | + const removedFolderPaths = this.convertWorkspaceFoldersToFilePaths(event.removed) |
| 87 | + this.removeFromPath(removedFolderPaths, matlabConnection) |
| 88 | + |
| 89 | + // log warning if primary workspace folder was removed |
| 90 | + if (this.isCwdInPaths(removedFolderPaths, cwd)) { |
| 91 | + Logger.warn('The current working directory was removed from the workspace.') |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private setWorkingDirectory (path: string, matlabConnection: MatlabConnection): void { |
| 96 | + Logger.log(`CWD set to: ${path}`) |
| 97 | + |
| 98 | + matlabConnection.publish(this.CD_REQUEST_CHANNEL, { |
| 99 | + path |
| 100 | + }) |
| 101 | + } |
| 102 | + |
| 103 | + private getCurrentWorkingDirectory (matlabConnection: MatlabConnection): Promise<string> { |
| 104 | + const channelId = matlabConnection.getChannelId() |
| 105 | + const channel = `${this.PWD_RESPONSE_CHANNEL}/${channelId}` |
| 106 | + |
| 107 | + return new Promise<string>(resolve => { |
| 108 | + const responseSub = matlabConnection.subscribe(channel, message => { |
| 109 | + const cwd = message as string |
| 110 | + matlabConnection.unsubscribe(responseSub) |
| 111 | + resolve(path.normalize(cwd)) |
| 112 | + }) |
| 113 | + |
| 114 | + matlabConnection.publish(this.PWD_REQUEST_CHANNEL, { |
| 115 | + channelId |
| 116 | + }) |
| 117 | + }) |
| 118 | + } |
| 119 | + |
| 120 | + private addToPath (paths: string[], matlabConnection: MatlabConnection): void { |
| 121 | + if (paths.length === 0) return |
| 122 | + |
| 123 | + Logger.log(`Adding workspace folder(s) to the MATLAB Path: \n\t${paths.join('\n\t')}`) |
| 124 | + matlabConnection.publish(this.ADDPATH_REQUEST_CHANNEL, { |
| 125 | + paths |
| 126 | + }) |
| 127 | + } |
| 128 | + |
| 129 | + private removeFromPath (paths: string[], matlabConnection: MatlabConnection): void { |
| 130 | + if (paths.length === 0) return |
| 131 | + |
| 132 | + Logger.log(`Removing workspace folder(s) from the MATLAB Path: \n\t${paths.join('\n\t')}`) |
| 133 | + matlabConnection.publish(this.RMPATH_REQUEST_CHANNEL, { |
| 134 | + paths |
| 135 | + }) |
| 136 | + } |
| 137 | + |
| 138 | + private convertWorkspaceFoldersToFilePaths (workspaceFolders: WorkspaceFolder[]): string[] { |
| 139 | + return workspaceFolders.map(folder => { |
| 140 | + let uri = decodeURIComponent(folder.uri) |
| 141 | + uri = uri.replace('file:///', '') |
| 142 | + return path.normalize(uri) |
| 143 | + }); |
| 144 | + } |
| 145 | + |
| 146 | + private isCwdInPaths (folderPaths: string[], cwd: string): boolean { |
| 147 | + if (os.platform() === 'win32') { |
| 148 | + // On Windows, paths are case-insensitive |
| 149 | + return folderPaths.some(folderPath => folderPath.toLowerCase() === cwd.toLowerCase()); |
| 150 | + } else { |
| 151 | + // On Unix-like systems, paths are case-sensitive |
| 152 | + return folderPaths.includes(cwd); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments