Skip to content

Commit 9633608

Browse files
authored
Merge pull request microsoft#102 from mousetraps/api
microsoft#101 Add parser path setting to syntax visualizer
2 parents 5d5770a + 537c245 commit 9633608

File tree

7 files changed

+47
-33
lines changed

7 files changed

+47
-33
lines changed

syntax-visualizer/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@ VSCode Extension that demonstrates some of the basic usage and functionality of
1212
1. npm install `client`/`server` dependencies, run Extension
1313
2. open PHP file from `example` folder, and `*.ast` file to the side
1414
3. check out error information, and corresponding AST
15-
4. make some changes, and save to view updates
15+
4. make some changes, and save to view updates
16+
17+
## Settings
18+
* `php.syntaxVisualizer.parserPath`: set this path to use a different parser version than the one bundled with the
19+
syntax visualizer. For instance, after `git clone https://github.com/Microsoft/tolerant-php-parser`, set
20+
`php.syntaxVisualizer.parserPath` to the absolute directory of the `tolerant-php-parser/src` folder. This will enable you to easily
21+
debug the parser during development.

syntax-visualizer/client/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
"type": "object",
2121
"title": "Example configuration",
2222
"properties": {
23-
"languageServerExample.maxNumberOfProblems": {
24-
"type": "number",
25-
"default": 100,
26-
"description": "Controls the maximum number of problems produced by the server."
23+
"php.syntaxVisualizer.parserPath": {
24+
"type": ["string", "null"],
25+
"default": null,
26+
"description": "Absolute path to the parser source folder (should contain bootstrap.php)."
2727
}
2828
}
2929
}

syntax-visualizer/client/package.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
rmdirRecursive($outDir);
1717
}
1818

19+
// Package the latest parser source file archive from msft/master
20+
// This prevents old or in-progress work from being accidentally packaged with the extension.
21+
// TODO - eventually add more configuration options
22+
$updateMasterOutput = `git diff master msft/master 2>&1`;
23+
if (\count($updateMasterOutput) > 0) {
24+
throw new Exception("master branch is not up to date with msft/master");
25+
}
26+
1927
$root = exec("git rev-parse --show-toplevel");
2028
exec("cd $root && git archive --format zip --output \"$outZip\" master");
2129

Binary file not shown.

syntax-visualizer/client/src/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export function activate(context: ExtensionContext) {
2828
// Register the server for PHP documents
2929
documentSelector: ['php'],
3030
synchronize: {
31-
// Synchronize the setting section 'languageServerExample' to the server
32-
configurationSection: 'languageServerExample',
31+
// Synchronize the setting section 'php' to the server
32+
configurationSection: 'php',
3333
// Notify the server about file changes to '.clientrc files contain in the workspace
3434
fileEvents: workspace.createFileSystemWatcher('**/.clientrc')
3535
}

syntax-visualizer/server/src/parse.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,7 @@
88
use Microsoft\PhpParser\DiagnosticsProvider;
99
use Microsoft\PhpParser\PositionUtilities;
1010

11-
$configFile = __DIR__ . "/config.php";
12-
if (file_exists($configFile)) {
13-
require_once($configFile);
14-
}
15-
16-
if (!isset($GLOBALS["PARSER_PATH"])) {
17-
$GLOBALS["PARSER_PATH"] = __DIR__ . "/../../../src/";
18-
}
11+
$GLOBALS["PARSER_PATH"] = isset($argv[2]) ? $argv[2] . "/" : __DIR__ . "/../../../src/";
1912

2013
require_once($GLOBALS["PARSER_PATH"] . "bootstrap.php");
2114

syntax-visualizer/server/src/server.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ import {
1212
CompletionItem, CompletionItemKind
1313
} from 'vscode-languageserver';
1414

15+
var os = require('os');
16+
var execSync = require('child_process').execSync;
17+
var querystring = require('querystring');
18+
var path = require('path');
19+
var fs = require('fs');
20+
1521
// Create a connection for the server. The connection uses Node's IPC as a transport
1622
let connection: IConnection = createConnection(new IPCMessageReader(process), new IPCMessageWriter(process));
1723

@@ -44,50 +50,51 @@ documents.onDidSave((change) => {
4450
});
4551

4652
// The settings interface describe the server relevant settings part
47-
interface Settings {
48-
languageServerExample: ExampleSettings;
53+
interface PhpSettings {
54+
syntaxVisualizer: SyntaxVisualizerSettings;
4955
}
5056

51-
// These are the example settings we defined in the client's package.json
52-
// file
53-
interface ExampleSettings {
54-
maxNumberOfProblems: number;
57+
interface SyntaxVisualizerSettings {
58+
parserPath: string;
5559
}
5660

57-
// hold the maxNumberOfProblems setting
58-
let maxNumberOfProblems: number;
61+
// hold the parserSrc setting
62+
let parserPath: string | null;
5963
// The settings have changed. Is send on server activation
6064
// as well.
6165
connection.onDidChangeConfiguration((change) => {
62-
let settings = <Settings>change.settings;
63-
maxNumberOfProblems = settings.languageServerExample.maxNumberOfProblems || 100;
66+
let syntaxVisualizerSettings = <SyntaxVisualizerSettings>change.settings.php.syntaxVisualizer;
67+
let fallbackParserPath = fs.existsSync(`${__dirname}/parser`)
68+
? `${__dirname}/parser/src`
69+
: `${__dirname}/../../../src`;
70+
71+
parserPath = syntaxVisualizerSettings && syntaxVisualizerSettings.parserPath
72+
? syntaxVisualizerSettings.parserPath
73+
: fallbackParserPath;
74+
console.log(`parser path: ${parserPath}`);
75+
6476
// Revalidate any open text documents
6577
documents.all().forEach(validateTextDocument);
6678
});
6779

6880
function validateTextDocument(textDocument: TextDocument): void {
69-
var os = require('os');
70-
var execSync = require('child_process').execSync;
71-
var querystring = require('querystring');
72-
var path = require('path');
7381
var fileToRead = path.normalize(querystring.unescape(textDocument.uri)).substr(os.platform() === 'win32' ? 6 : 5);
7482
if (fileToRead.startsWith("x")) {
7583
return;
7684
}
77-
var fs = require('fs');
7885
var cmd = fs.existsSync(`${__dirname}/parser`)
79-
? `php ${__dirname}/parse.php ${fileToRead}`
86+
? `php ${__dirname}/parse.php`
8087
: `php ${__dirname}/../../server/src/parse.php`;
8188

82-
cmd += ` ${fileToRead}`;
89+
cmd += ` ${fileToRead} ${parserPath}`;
8390
var out = execSync(cmd).toString();
8491
var outErrors = JSON.parse(out);
8592
let diagnostics: Diagnostic[] = [];
8693
let lines = textDocument.getText().split(/\n/g);
8794

8895
let allErrors = outErrors;
8996

90-
for (var i = 0; i < allErrors.length && i < maxNumberOfProblems; i++) {
97+
for (var i = 0; i < allErrors.length; i++) {
9198
let error = allErrors[i];
9299
diagnostics.push({
93100
severity: DiagnosticSeverity.Error,

0 commit comments

Comments
 (0)