Skip to content

Commit

Permalink
Add prettier (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehedbor authored May 22, 2024
1 parent a52ced7 commit 8e4a9bf
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 132 deletions.
13 changes: 13 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
printWidth: 100,
tabWidth: 2,
useTabs: false,
semi: true,
singleQuote: true,
quoteProps: 'as-needed',
trailingComma: 'all',
bracketSpacing: true,
arrowParens: 'always',
proseWrap: 'preserve',
endOfLine: 'lf',
};
12 changes: 11 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,15 @@
"typescript.preferences.quoteStyle": "single",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
}
},
"prettier.configPath": "./.prettierrc.js",
"cSpell.words": [
"metamodelica",
"Modelica",
"nodelib",
"OSMC",
"redeclaration"
],
"files.insertFinalNewline": true,
"files.trimTrailingWhitespace": true,
}
20 changes: 9 additions & 11 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
LanguageClient,
LanguageClientOptions,
ServerOptions,
TransportKind
TransportKind,
} from 'vscode-languageclient/node';
import { getFileExtension, getLanguage } from './getLanguage';
import { fstat } from 'fs';
Expand All @@ -49,7 +49,7 @@ let client: LanguageClient;

export function activate(context: ExtensionContext) {
// Register event listener to set language for '.mo' files.
const checkedFiles: { [id: string]: boolean} = {};
const checkedFiles: { [id: string]: boolean } = {};
workspace.onDidOpenTextDocument((document: TextDocument) => {
if (checkedFiles[document.fileName]) {
return;
Expand All @@ -73,9 +73,7 @@ export function activate(context: ExtensionContext) {
});

// The server is implemented in node, point to packed module
const serverModule = context.asAbsolutePath(
path.join('out', 'server.js')
);
const serverModule = context.asAbsolutePath(path.join('out', 'server.js'));
if (!fs.existsSync(serverModule)) {
throw new Error(`Can't find server module in ${serverModule}`);
}
Expand All @@ -87,7 +85,7 @@ export function activate(context: ExtensionContext) {
debug: {
module: serverModule,
transport: TransportKind.ipc,
}
},
};

// Options to control the language client
Expand All @@ -96,21 +94,21 @@ export function activate(context: ExtensionContext) {
documentSelector: [
{
language: 'modelica',
scheme: 'file'
}
scheme: 'file',
},
],
synchronize: {
// Notify the server about file changes to '.clientrc files contained in the workspace
fileEvents: workspace.createFileSystemWatcher('**/.clientrc')
}
fileEvents: workspace.createFileSystemWatcher('**/.clientrc'),
},
};

// Create the language client and start the client.
client = new LanguageClient(
'modelicaLanguageServer',
'Modelica Language Server',
serverOptions,
clientOptions
clientOptions,
);

// Start the client. This will also launch the server
Expand Down
2 changes: 1 addition & 1 deletion client/src/getLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import * as path from 'path';
import { TextDocument } from 'vscode';

type LanguageTypes = 'modelica' | 'metamodelica' | 'unknown'
type LanguageTypes = 'modelica' | 'metamodelica' | 'unknown';

export function getFileExtension(document: TextDocument): string | undefined {
const uri = document.uri;
Expand Down
2 changes: 1 addition & 1 deletion client/src/test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export async function activate(docUri: vscode.Uri) {
}

async function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
return new Promise((resolve) => setTimeout(resolve, ms));
}

export const getDocPath = (p: string) => {
Expand Down
8 changes: 4 additions & 4 deletions client/src/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function run(): Promise<void> {
// Create the mocha test
const mocha = new Mocha({
ui: 'tdd',
color: true
color: true,
});
mocha.timeout(100000);

Expand All @@ -54,11 +54,11 @@ export function run(): Promise<void> {
}

// Add files to the test suite
files.forEach(f => mocha.addFile(path.resolve(testsRoot, f)));
files.forEach((f) => mocha.addFile(path.resolve(testsRoot, f)));

try {
// Run the mocha test
mocha.run(failures => {
mocha.run((failures) => {
if (failures > 0) {
reject(new Error(`${failures} tests failed.`));
} else {
Expand All @@ -71,4 +71,4 @@ export function run(): Promise<void> {
}
});
});
}
}
85 changes: 52 additions & 33 deletions client/src/test/symbolinformation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,86 +43,105 @@ suite('Symbol Information', () => {
test('onDocumentSymbol()', async () => {
const documentSymbols: vscode.DocumentSymbol[] = [
new vscode.DocumentSymbol(
'MyLibrary',
'',
vscode.SymbolKind.Package,
new vscode.Range(new vscode.Position(0, 0), new vscode.Position(6, 13)),
new vscode.Range(new vscode.Position(0, 0), new vscode.Position(6, 13)),
)
'MyLibrary',
'',
vscode.SymbolKind.Package,
new vscode.Range(new vscode.Position(0, 0), new vscode.Position(6, 13)),
new vscode.Range(new vscode.Position(0, 0), new vscode.Position(6, 13)),
),
];
documentSymbols[0].children.push(
new vscode.DocumentSymbol(
'M',
'',
vscode.SymbolKind.Class,
new vscode.Range(new vscode.Position(1, 2), new vscode.Position(5, 7)),
new vscode.Range(new vscode.Position(1, 2), new vscode.Position(5, 7))
));
'M',
'',
vscode.SymbolKind.Class,
new vscode.Range(new vscode.Position(1, 2), new vscode.Position(5, 7)),
new vscode.Range(new vscode.Position(1, 2), new vscode.Position(5, 7)),
),
);

await testSymbolInformation(docUri, documentSymbols);
});
});

async function testSymbolInformation(
docUri: vscode.Uri,
expectedDocumentSymbols: vscode.DocumentSymbol[]
expectedDocumentSymbols: vscode.DocumentSymbol[],
) {
await activate(docUri);

// Execute `vscode.executeDocumentSymbolProvider` to get file outline
const actualSymbolInformation = await vscode.commands.executeCommand<vscode.DocumentSymbol[]>("vscode.executeDocumentSymbolProvider", docUri);
const actualSymbolInformation = await vscode.commands.executeCommand<vscode.DocumentSymbol[]>(
'vscode.executeDocumentSymbolProvider',
docUri,
);

//printDocumentSymbols(actualSymbolInformation);
assertDocumentSymbolsEqual(expectedDocumentSymbols, actualSymbolInformation);
}

function printDocumentSymbols(documentSymbols: vscode.DocumentSymbol[]) {
documentSymbols.forEach((symbol, index) => {
console.log(`Document Symbol ${index + 1}:`);
console.log(`Name: ${symbol.name}`);
console.log(`Kind: ${vscode.SymbolKind[symbol.kind]}`);
console.log(`Range: ${symbol.range.start.line}:${symbol.range.start.character}, ${symbol.range.end.line}:${symbol.range.end.character}`);
console.log(`SelectionRange: ${symbol.selectionRange.start.line}:${symbol.selectionRange.start.character}, ${symbol.selectionRange.end.line}:${symbol.selectionRange.end.character}`);
console.log('Children:');

if (symbol.children && symbol.children.length > 0) {
printDocumentSymbols(symbol.children);
}

console.log('---');
console.log(`Document Symbol ${index + 1}:`);
console.log(`Name: ${symbol.name}`);
console.log(`Kind: ${vscode.SymbolKind[symbol.kind]}`);
console.log(
`Range: ${symbol.range.start.line}:${symbol.range.start.character}, ${symbol.range.end.line}:${symbol.range.end.character}`,
);
console.log(
`SelectionRange: ${symbol.selectionRange.start.line}:${symbol.selectionRange.start.character}, ${symbol.selectionRange.end.line}:${symbol.selectionRange.end.character}`,
);
console.log('Children:');

if (symbol.children && symbol.children.length > 0) {
printDocumentSymbols(symbol.children);
}

console.log('---');
});
}

function assertDocumentSymbolsEqual(expected: vscode.DocumentSymbol[], actual: vscode.DocumentSymbol[]) {
function assertDocumentSymbolsEqual(
expected: vscode.DocumentSymbol[],
actual: vscode.DocumentSymbol[],
) {
assert.strictEqual(expected.length, actual.length, 'Array lengths do not match.');

for (let i = 0; i < expected.length; i++) {
const expectedSymbol = expected[i];
const actualSymbol = actual[i];

assert.strictEqual(expectedSymbol.name, actualSymbol.name, `Symbol names do not match at index ${i}.`);
assert.strictEqual(expectedSymbol.kind, actualSymbol.kind, `Symbol kinds do not match at index ${i}.`);
assert.strictEqual(
expectedSymbol.name,
actualSymbol.name,
`Symbol names do not match at index ${i}.`,
);
assert.strictEqual(
expectedSymbol.kind,
actualSymbol.kind,
`Symbol kinds do not match at index ${i}.`,
);

assert.strictEqual(
expectedSymbol.range.start.line,
actualSymbol.range.start.line,
`Symbol start line does not match at index ${i}.`
`Symbol start line does not match at index ${i}.`,
);
assert.strictEqual(
expectedSymbol.range.start.character,
actualSymbol.range.start.character,
`Symbol start character does not match at index ${i}.`
`Symbol start character does not match at index ${i}.`,
);

assert.strictEqual(
expectedSymbol.range.end.line,
actualSymbol.range.end.line,
`Symbol end line does not match at index ${i}.`
`Symbol end line does not match at index ${i}.`,
);
assert.strictEqual(
expectedSymbol.range.end.character,
actualSymbol.range.end.character,
`Symbol end character does not match at index ${i}.`
`Symbol end character does not match at index ${i}.`,
);

// Recursive check for children symbols
Expand Down
28 changes: 13 additions & 15 deletions server/src/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,21 @@ import { TextDocument } from 'vscode-languageserver-textdocument';

import Parser = require('web-tree-sitter');

import {
getAllDeclarationsInTree
} from './util/declarations';
import { getAllDeclarationsInTree } from './util/declarations';
import { logger } from './util/logger';

type AnalyzedDocument = {
document: TextDocument,
declarations: LSP.SymbolInformation[],
tree: Parser.Tree
}
document: TextDocument;
declarations: LSP.SymbolInformation[];
tree: Parser.Tree;
};

export default class Analyzer {
private parser: Parser;
private uriToAnalyzedDocument: Record<string, AnalyzedDocument | undefined> = {};
#parser: Parser;
#uriToAnalyzedDocument: Record<string, AnalyzedDocument | undefined> = {};

constructor (parser: Parser) {
this.parser = parser;
public constructor(parser: Parser) {
this.#parser = parser;
}

public analyze(document: TextDocument): LSP.Diagnostic[] {
Expand All @@ -70,17 +68,17 @@ export default class Analyzer {
const fileContent = document.getText();
const uri = document.uri;

const tree = this.parser.parse(fileContent);
const tree = this.#parser.parse(fileContent);
logger.debug(tree.rootNode.toString());

// Get declarations
const declarations = getAllDeclarationsInTree(tree, uri);

// Update saved analysis for document uri
this.uriToAnalyzedDocument[uri] = {
this.#uriToAnalyzedDocument[uri] = {
document,
declarations,
tree
tree,
};

return diagnostics;
Expand All @@ -92,7 +90,7 @@ export default class Analyzer {
* TODO: convert to DocumentSymbol[] which is a hierarchy of symbols found in a given text document.
*/
public getDeclarationsForUri(uri: string): LSP.SymbolInformation[] {
const tree = this.uriToAnalyzedDocument[uri]?.tree;
const tree = this.#uriToAnalyzedDocument[uri]?.tree;

if (!tree?.rootNode) {
return [];
Expand Down
2 changes: 1 addition & 1 deletion server/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import * as path from 'path';
*/
export async function initializeParser(): Promise<Parser> {
await Parser.init();
const parser = new Parser;
const parser = new Parser();

const modelicaWasmFile = path.join(__dirname, 'tree-sitter-modelica.wasm');
if (!fs.existsSync(modelicaWasmFile)) {
Expand Down
Loading

0 comments on commit 8e4a9bf

Please sign in to comment.