Skip to content

Commit

Permalink
Introduce --map-folder switch
Browse files Browse the repository at this point in the history
And create PathMapper based on its value
  • Loading branch information
weirdan committed Jul 23, 2023
1 parent 815c0ac commit 38025d1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 10 deletions.
67 changes: 64 additions & 3 deletions src/Psalm/Internal/Cli/LanguageServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Psalm\Internal\IncludeCollector;
use Psalm\Internal\LanguageServer\ClientConfiguration;
use Psalm\Internal\LanguageServer\LanguageServer as LanguageServerLanguageServer;
use Psalm\Internal\LanguageServer\PathMapper;
use Psalm\Report;

use function array_key_exists;
Expand Down Expand Up @@ -75,6 +76,7 @@ public static function run(array $argv): void
'find-dead-code',
'help',
'root:',
'map-folder::',
'use-ini-defaults',
'version',
'tcp:',
Expand Down Expand Up @@ -127,6 +129,14 @@ static function (string $arg) use ($valid_long_options): void {

// get options from command line
$options = getopt(implode('', $valid_short_options), $valid_long_options);
if ($options === false) {
// shouldn't really happen, but just in case
fwrite(
STDERR,
'Failed to get CLI args' . PHP_EOL,
);
exit(1);
}

if (!array_key_exists('use-ini-defaults', $options)) {
ini_set('display_errors', '1');
Expand Down Expand Up @@ -169,6 +179,14 @@ static function (string $arg) use ($valid_long_options): void {
-r, --root
If running Psalm globally you'll need to specify a project root. Defaults to cwd
--map-folder[=SERVER_FOLDER:CLIENT_FOLDER]
Specify folder to map between the client and the server. Use this when the client
and server have different views of the filesystem (e.g. in a docker container).
Defaults to mapping the rootUri provided by the client to the server's cwd,
or `-r` if provided.
No mapping is done when this option is not specified.
--find-dead-code
Look for dead code
Expand Down Expand Up @@ -254,8 +272,6 @@ static function (string $arg) use ($valid_long_options): void {
$current_dir = $root_path . DIRECTORY_SEPARATOR;
}

$server_start_dir = $current_dir;

$vendor_dir = CliUtils::getVendorDir($current_dir);

$include_collector = new IncludeCollector();
Expand Down Expand Up @@ -293,6 +309,8 @@ static function (string $arg) use ($valid_long_options): void {

setlocale(LC_CTYPE, 'C');

$path_mapper = self::createPathMapper($options, $current_dir);

$path_to_config = CliUtils::getPathToConfig($options);

if (isset($options['tcp'])) {
Expand Down Expand Up @@ -396,6 +414,49 @@ static function (string $arg) use ($valid_long_options): void {
$clientConfiguration->TCPServerAddress = $options['tcp'] ?? null;
$clientConfiguration->TCPServerMode = isset($options['tcp-server']);

LanguageServerLanguageServer::run($config, $clientConfiguration, $current_dir, $server_start_dir, $inMemory);
LanguageServerLanguageServer::run($config, $clientConfiguration, $current_dir, $path_mapper, $inMemory);
}

/** @param array<string,string|false|list<string|false>> $options */
private static function createPathMapper(array $options, string $server_start_dir): PathMapper
{
if (!isset($options['map-folder'])) {
// dummy no-op mapper
return new PathMapper('/', '/');
}

$map_folder = $options['map-folder'];

if ($map_folder === false) {
// autoconfigured mapper
return new PathMapper($server_start_dir, null);
}

if (is_string($map_folder)) {
if (strpos($map_folder, ':') === false) {
fwrite(
STDERR,
'invalid format for --map-folder option' . PHP_EOL,
);
exit(1);
}
/** @psalm-suppress PossiblyUndefinedArrayOffset we just checked that we have the separator*/
[$server_dir, $client_dir] = explode(':', $map_folder, 2);
if (!strlen($server_dir) || !strlen($client_dir)) {
fwrite(
STDERR,
'invalid format for --map-folder option, '
. 'neither SERVER_FOLDER nor CLIENT_FOLDER can be empty' . PHP_EOL,
);
exit(1);
}
return new PathMapper($server_dir, $client_dir);
}

fwrite(
STDERR,
'--map-folder option can only be specified once' . PHP_EOL,
);
exit(1);
}
}
13 changes: 7 additions & 6 deletions src/Psalm/Internal/LanguageServer/LanguageServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function __construct(
Codebase $codebase,
ClientConfiguration $clientConfiguration,
Progress $progress,
string $server_start_dir
PathMapper $path_mapper
) {
parent::__construct($this, '/');

Expand All @@ -163,6 +163,8 @@ public function __construct(

$this->codebase = $codebase;

$this->path_mapper = $path_mapper;

$this->protocolWriter = $writer;

$this->protocolReader = $reader;
Expand Down Expand Up @@ -245,7 +247,6 @@ function (): void {

$this->client = new LanguageClient($reader, $writer, $this, $clientConfiguration);

$this->path_mapper = new PathMapper($server_start_dir, null);

$this->logInfo("Psalm Language Server ".PSALM_VERSION." has started.");
}
Expand All @@ -257,7 +258,7 @@ public static function run(
Config $config,
ClientConfiguration $clientConfiguration,
string $base_dir,
string $server_start_dir,
PathMapper $path_mapper,
bool $inMemory = false
): void {
$progress = new Progress();
Expand Down Expand Up @@ -330,7 +331,7 @@ public static function run(
$codebase,
$clientConfiguration,
$progress,
$server_start_dir,
$path_mapper,
);
Loop::run();
} elseif ($clientConfiguration->TCPServerMode && $clientConfiguration->TCPServerAddress) {
Expand All @@ -354,7 +355,7 @@ public static function run(
$codebase,
$clientConfiguration,
$progress,
$server_start_dir,
$path_mapper,
);
Loop::run();
}
Expand All @@ -368,7 +369,7 @@ public static function run(
$codebase,
$clientConfiguration,
$progress,
$server_start_dir,
$path_mapper,
);
Loop::run();
}
Expand Down
3 changes: 2 additions & 1 deletion tests/LanguageServer/DiagnosticTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Psalm\Internal\LanguageServer\ClientConfiguration;
use Psalm\Internal\LanguageServer\LanguageServer;
use Psalm\Internal\LanguageServer\Message;
use Psalm\Internal\LanguageServer\PathMapper;
use Psalm\Internal\LanguageServer\Progress;
use Psalm\Internal\Provider\FakeFileProvider;
use Psalm\Internal\Provider\Providers;
Expand Down Expand Up @@ -85,7 +86,7 @@ public function testSnippetSupportDisabled(): void
$this->codebase,
$clientConfiguration,
new Progress,
getcwd(),
new PathMapper(getcwd(), getcwd()),
);

$write->on('message', function (Message $message) use ($deferred, $server): void {
Expand Down

0 comments on commit 38025d1

Please sign in to comment.