Skip to content
This repository has been archived by the owner on Dec 1, 2024. It is now read-only.

Commit

Permalink
When running in LSP mode, use the specified root, not cwd
Browse files Browse the repository at this point in the history
Some implementations (e.g. ale) don't run it from the project root.
  • Loading branch information
fredemmott committed Aug 2, 2018
1 parent 758bae1 commit 986f489
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 67 deletions.
2 changes: 0 additions & 2 deletions src/__Private/LSPImpl/CodeActionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace Facebook\HHAST\__Private\LSPImpl;

use type Facebook\HHAST\__Private\LintRunConfig;
use namespace Facebook\HHAST\__Private\{LSP, LSPLib};
use namespace Facebook\HHAST\Linters;
use namespace HH\Lib\{C, Str, Vec};
Expand All @@ -20,7 +19,6 @@ final class CodeActionCommand extends LSPLib\CodeActionCommand {

public function __construct(
private LSPLib\Client $client,
private ?LintRunConfig $config,
private ServerState $state,
) {
}
Expand Down
8 changes: 2 additions & 6 deletions src/__Private/LSPImpl/DidChangeWatchedFilesNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

namespace Facebook\HHAST\__Private\LSPImpl;

use type Facebook\HHAST\__Private\{
LintRunConfig,
LintRunLSPPublishDiagnosticsEventHandler,
};
use type Facebook\HHAST\__Private\LintRunLSPPublishDiagnosticsEventHandler;
use namespace Facebook\HHAST\__Private\{LSP, LSPLib};
use namespace HH\Lib\{C, Str, Vec};

Expand All @@ -22,7 +19,6 @@ final class DidChangeWatchedFilesNotification

public function __construct(
private LSPLib\Client $client,
private ?LintRunConfig $config,
private ServerState $state,
) {
}
Expand Down Expand Up @@ -63,7 +59,7 @@ public function __construct(

await relint_uris_async(
new LintRunLSPPublishDiagnosticsEventHandler($this->client, $this->state),
$this->config,
$this->state->config,
$to_relint,
);
}
Expand Down
8 changes: 2 additions & 6 deletions src/__Private/LSPImpl/DidOpenTextDocumentNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

namespace Facebook\HHAST\__Private\LSPImpl;

use type Facebook\HHAST\__Private\{
LintRunConfig,
LintRunLSPPublishDiagnosticsEventHandler,
};
use type Facebook\HHAST\__Private\LintRunLSPPublishDiagnosticsEventHandler;
use namespace Facebook\HHAST\__Private\LSPLib;
use namespace HH\Lib\Str;

Expand All @@ -22,7 +19,6 @@ final class DidOpenTextDocumentNotification

public function __construct(
private LSPLib\Client $client,
private ?LintRunConfig $config,
private ServerState $state,
) {
}
Expand All @@ -41,7 +37,7 @@ public function __construct(

await relint_uri_async(
new LintRunLSPPublishDiagnosticsEventHandler($this->client, $this->state),
$this->config,
$this->state->config,
$uri,
);
}
Expand Down
8 changes: 2 additions & 6 deletions src/__Private/LSPImpl/DidSaveTextDocumentNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

namespace Facebook\HHAST\__Private\LSPImpl;

use type Facebook\HHAST\__Private\{
LintRunConfig,
LintRunLSPPublishDiagnosticsEventHandler,
};
use type Facebook\HHAST\__Private\LintRunLSPPublishDiagnosticsEventHandler;
use namespace Facebook\HHAST\__Private\LSPLib;
use namespace HH\Lib\Str;

Expand All @@ -22,7 +19,6 @@ final class DidSaveTextDocumentNotification

public function __construct(
private LSPLib\Client $client,
private ?LintRunConfig $config,
private ServerState $state,
) {
}
Expand All @@ -36,7 +32,7 @@ public function __construct(

await relint_uri_async(
new LintRunLSPPublishDiagnosticsEventHandler($this->client, $this->state),
$this->config,
$this->state->config,
$uri,
);
}
Expand Down
13 changes: 13 additions & 0 deletions src/__Private/LSPImpl/InitializeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

use namespace Facebook\TypeAssert;
use namespace Facebook\HHAST\__Private\{LSP, LSPLib};
use namespace HH\Lib\Str;
use type Facebook\HHAST\__Private\LintRunConfig;

final class InitializeCommand extends LSPLib\InitializeCommand<ServerState> {

Expand Down Expand Up @@ -43,10 +45,21 @@ final class InitializeCommand extends LSPLib\InitializeCommand<ServerState> {
);

$lint_mode = $options['lintMode'] ?? null;

if ($lint_mode !== null) {
$this->state->lintMode = $lint_mode;
}

invariant($this->state->config === null, 'Tried to set config twice');
$uri = $p['rootUri'];
if ($uri === null) {
$uri = 'file://'.\getcwd();
}
if (Str\starts_with($uri, 'file://')) {
$path = Str\strip_prefix($uri, 'file://');
$this->state->config = LintRunConfig::getForPath($path);
}

return await parent::executeAsync($p);
}
}
31 changes: 12 additions & 19 deletions src/__Private/LSPImpl/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

use type Facebook\HHAST\__Private\{
Asio\AsyncPoll,
LintRunConfig,
LintRunLSPPublishDiagnosticsEventHandler,
LintRun,
};
Expand All @@ -21,11 +20,7 @@
use namespace HH\Lib\Str;

final class Server extends LSPLib\Server<ServerState> {
public function __construct(
private ITerminal $terminal,
private ?LintRunConfig $config,
private vec<string> $roots,
) {
public function __construct(private ITerminal $terminal) {
parent::__construct(new LSPImpl\Client($terminal), new ServerState());
}

Expand All @@ -34,7 +29,7 @@ protected function getSupportedServerCommands(): vec<LSPLib\ServerCommand> {
return vec[
new LSPImpl\InitializeCommand($this->state),
new LSPLib\ShutdownCommand($this->state),
new LSPImpl\CodeActionCommand($this->client, $this->config, $this->state),
new LSPImpl\CodeActionCommand($this->client, $this->state),
new LSPImpl\ExecuteCommandCommand($this->client),
];
}
Expand All @@ -45,19 +40,10 @@ protected function getSupportedClientNotifications(
return vec[
new LSPImpl\DidChangeWatchedFilesNotification(
$this->client,
$this->config,
$this->state,
),
new LSPImpl\DidSaveTextDocumentNotification(
$this->client,
$this->config,
$this->state,
),
new LSPImpl\DidOpenTextDocumentNotification(
$this->client,
$this->config,
$this->state,
),
new LSPImpl\DidSaveTextDocumentNotification($this->client, $this->state),
new LSPImpl\DidOpenTextDocumentNotification($this->client, $this->state),
new LSPImpl\DidCloseTextDocumentNotification($this->client, $this->state),
new LSPImpl\ExitNotification($this->state),
new LSPImpl\InitializedNotification($this->client, $this->state),
Expand Down Expand Up @@ -116,7 +102,14 @@ protected function getSupportedClientNotifications(

$handler =
new LintRunLSPPublishDiagnosticsEventHandler($this->client, $this->state);
await (new LintRun($this->config, $handler, $this->roots))->runAsync();
await (
new LintRun(
$this->state->config,
$handler,
$this->state->config?->getRoots() ?? vec[],
)
)
->runAsync();
}

private async function readMessageAsync(): Awaitable<string> {
Expand Down
2 changes: 2 additions & 0 deletions src/__Private/LSPImpl/ServerState.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

use namespace Facebook\HHAST\Linters;
use namespace Facebook\HHAST\__Private\LSPLib;
use type Facebook\HHAST\__Private\LintRunConfig;

final class ServerState extends LSPLib\ServerState {
public ?LintRunConfig $config = null;
public LintMode $lintMode = LintMode::WHOLE_PROJECT;
public keyset<string> $openFiles = keyset[];
public dict<string, vec<Linters\LintError>> $lintErrors = dict[];
Expand Down
17 changes: 10 additions & 7 deletions src/__Private/LintRunConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,20 @@ private static function getNamedLinterGroup(
}
}

private function __construct(private self::TConfigFile $configFile) {
private function __construct(
private string $projectRoot,
private self::TConfigFile $configFile,
) {
}

<<__Memoize>>
private static function getFromConfigFile(string $path): this {
return new self(self::getConfigFromFile($path));
return new self(\dirname($path), self::getConfigFromFile($path));
}

<<__Memoize>>
private static function getDefault(): this {
return new self(shape('roots' => vec[]));
return new self(\getcwd(), shape('roots' => vec[]));
}

public static function getForPath(string $path): this {
Expand All @@ -135,13 +138,13 @@ private static function getForPathImpl(string $path): this {
}

public function getRoots(): vec<string> {
return $this->configFile['roots'];
return
Vec\map($this->configFile['roots'], $dir ==> $this->projectRoot.'/'.$dir);
}

public function getConfigForFile(string $file_path): self::TFileConfig {
$roots = Vec\map($this->getRoots(), $s ==> Str\strip_suffix($s, '/').'/');
$file_path =
Str\strip_prefix($file_path, Str\strip_suffix(\getcwd(), '/').'/');
$roots = Vec\map($this->configFile['roots'], $s ==> Str\strip_suffix($s, '/').'/');
$file_path = Str\strip_prefix($file_path, $this->projectRoot.'/');
if (
$roots !== vec[] &&
!C\any($roots, $root ==> Str\starts_with($file_path, $root))
Expand Down
44 changes: 23 additions & 21 deletions src/__Private/LinterCLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,28 @@ protected function getSupportedOptions(): vec<CLIOptions\CLIOption> {
}

private async function mainImplAsync(): Awaitable<int> {
$terminal = $this->getTerminal();
$log_prefix = $this->ioLogPrefix;
if ($log_prefix !== null) {
$terminal = new Terminal(
new LoggingInputTap(
$terminal->getStdin(),
\fopen($log_prefix.'in', 'w+'),
),
new LoggingOutputTap(
$terminal->getStdout(),
\fopen($log_prefix.'out', 'w+'),
),
new LoggingOutputTap(
$terminal->getStderr(),
\fopen($log_prefix.'err', 'w+'),
),
);
}
if ($this->mode === LinterCLIMode::LSP) {
return await (new LSPImpl\Server($terminal))->mainAsync();
}

$err = $this->getStderr();
$roots = $this->getArguments();

Expand Down Expand Up @@ -122,25 +144,6 @@ protected function getSupportedOptions(): vec<CLIOptions\CLIOption> {
$config = null;
}

$terminal = $this->getTerminal();
$log_prefix = $this->ioLogPrefix;
if ($log_prefix !== null) {
$terminal = new Terminal(
new LoggingInputTap(
$terminal->getStdin(),
\fopen($log_prefix.'in', 'w+'),
),
new LoggingOutputTap(
$terminal->getStdout(),
\fopen($log_prefix.'out', 'w+'),
),
new LoggingOutputTap(
$terminal->getStderr(),
\fopen($log_prefix.'err', 'w+'),
),
);
}

switch ($this->mode) {
case LinterCLIMode::PLAIN:
$error_handler = new LintRunCLIEventHandler($terminal);
Expand All @@ -149,8 +152,7 @@ protected function getSupportedOptions(): vec<CLIOptions\CLIOption> {
$error_handler = new LintRunJSONEventHandler($terminal);
break;
case LinterCLIMode::LSP:
return await (new LSPImpl\Server($terminal, $config, $roots))
->mainAsync();
invariant_violation('should have returned earlier');
}

try {
Expand Down

0 comments on commit 986f489

Please sign in to comment.