Skip to content

Commit

Permalink
Merge branch 'support-document-symbol-psl-3' into support-document-sy…
Browse files Browse the repository at this point in the history
…mbol-psl
  • Loading branch information
yubing744 committed Jun 25, 2022
2 parents 3558e5d + 081f93e commit 0be56e1
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ jobs:
gem install awesome_bot
# Don't look in git or target dirs. Don't check png, bib, tex, js, or shell files
# We allow links to be redirects, allow duplicates, and we also allow Too Many Requests (429) errors
find . -not \( -path "./.git*" -prune \) -not \( -path "./target" -prune \) -type f -not -name "*.png" -not -name "*.sh" -not -name "*.bib" -not -name "*.tex" -not -name "*.js" | while read arg; do awesome_bot --allow-redirect --allow-dupe --allow 429 --skip-save-results $arg; done
find . -not \( -path "./.git*" -prune \) -not \( -path "./target" -prune \) -type f -not -name "*.png" -not -name "*.sh" -not -name "*.bib" -not -name "*.tex" -not -name "*.js" -not -name "package-lock.json" | while read arg; do awesome_bot --allow-redirect --allow-dupe --allow 429 --skip-save-results $arg; done
build-move-cli-docker-image:
name: Build Docker image for the Move CLI
Expand Down
2 changes: 1 addition & 1 deletion language/move-analyzer/editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
}
},
"scripts": {
"clean": "rm -rf ./out",
"clean": "rm -rf ./out",
"compile": "tsc -p ./ && cd ../../ && cargo build",
"watch": "tsc -watch -p ./",
"lint": "tsfmt --verify && eslint . --ext ts --max-warnings 0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export async function textDocumentDocumentSymbol(context: Readonly<Context>, par
return Promise.reject(new Error('No language client connected.'));
}

// Wait for the language client to be ready.
await context.onReady();
// Wait for the language server's symbolicator to be ready.
await context.onSymbolicatorReady();

// Send the request to the language client.
return client.sendRequest(lc.DocumentSymbolRequest.type, params);
Expand Down
30 changes: 15 additions & 15 deletions language/move-analyzer/editors/code/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ export class OnReady {
export class Context {
private _client: lc.LanguageClient | undefined;

private _ready: boolean;
private _symbolicatorReady: boolean;

private readonly _onReadyCallbacks: Array<OnReady>;
private readonly _onSymbolicatorReadyCallbacks: Array<OnReady>;

private constructor(
private readonly extensionContext: Readonly<vscode.ExtensionContext>,
readonly configuration: Readonly<Configuration>,
client: lc.LanguageClient | undefined = undefined,
) {
this._client = client;
this._ready = false;
this._onReadyCallbacks = [];
this._symbolicatorReady = false;
this._onSymbolicatorReadyCallbacks = [];
}

static create(
Expand Down Expand Up @@ -151,9 +151,9 @@ export class Context {
/* eslint @typescript-eslint/no-unsafe-member-access: off */
if (params[0] !== undefined && params[0].event_type === 'SymbolicatorEvent') {
if (params[0].event_data.result === 'success') {
this._resoleCallbacks();
this._resoleSymbolicatorCallbacks();
} else {
this._rejectCallbacks(params[0].event_data.result);
this._rejectSymbolicatorCallbacks(params[0].event_data.result);
}
}
});
Expand All @@ -172,27 +172,27 @@ export class Context {
}

/**
* Returns whether the language server is ready to accept requests.
* Returns whether the language server's symbolicator is ready to accept requests.
*
* @returns Promise<void>
*
*/
async onReady(): Promise<void> {
if (this._ready) {
async onSymbolicatorReady(): Promise<void> {
if (this._symbolicatorReady) {
return Promise.resolve();
}

const onReady = new OnReady();
this._onReadyCallbacks.push(onReady);
this._onSymbolicatorReadyCallbacks.push(onReady);
return onReady.wait();
}

_resoleCallbacks(): void {
this._ready = true;
this._onReadyCallbacks.forEach(callback => callback.resolve());
_resoleSymbolicatorCallbacks(): void {
this._symbolicatorReady = true;
this._onSymbolicatorReadyCallbacks.forEach(callback => callback.resolve());
}

_rejectCallbacks(reason: string): void {
this._onReadyCallbacks.forEach(callback => callback.reject(reason));
_rejectSymbolicatorCallbacks(reason: string): void {
this._onSymbolicatorReadyCallbacks.forEach(callback => callback.reject(reason));
}
} // Context
35 changes: 4 additions & 31 deletions language/move-analyzer/editors/code/tests/lsp-demo/sources/M1.move
Original file line number Diff line number Diff line change
@@ -1,40 +1,13 @@
module Symbols::M1 {

struct SomeStruct has key, drop, store {
some_field: u64,
}

const SOME_CONST: u64 = 42;


fun unpack(s: SomeStruct): u64 {
let SomeStruct { some_field: value } = s;
value
}

fun cp(value: u64): u64 {
let ret = value;
ret
}

fun pack(): SomeStruct {
let ret = SomeStruct { some_field: SOME_CONST };
ret
}

fun other_mod_struct(): Symbols::M2::SomeOtherStruct {
Symbols::M2::some_other_struct(SOME_CONST)
}

use Symbols::M2::{Self, SomeOtherStruct};

fun other_mod_struct_import(): SomeOtherStruct {
M2::some_other_struct(7)
struct SomeOtherStruct has drop {
some_field: u64,
}

fun acq(addr: address): u64 acquires SomeStruct {
let val = borrow_global<SomeStruct>(addr);
val.some_field
public fun some_other_struct(v: u64): SomeOtherStruct {
SomeOtherStruct { some_field: v }
}

#[test]
Expand Down
15 changes: 0 additions & 15 deletions language/move-analyzer/editors/code/tests/lsp-demo/sources/M2.move

This file was deleted.

21 changes: 16 additions & 5 deletions language/move-analyzer/editors/code/tests/lsp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,19 @@ import * as path from 'path';
import * as vscode from 'vscode';
import * as lc from 'vscode-languageclient';

import { sleep } from './utils';

Mocha.suite('LSP', () => {
Mocha.test('textDocument/documentSymbol', async () => {
const ext = vscode.extensions.getExtension('move.move-analyzer');
assert.ok(ext);

await ext.activate();
await sleep(10);

// 1. get workdir
const workDir = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath ?? '';

// 2. open doc
const docs = await vscode.workspace.openTextDocument(path.join(workDir, 'sources/M1.move'));
await vscode.window.showTextDocument(docs);
await sleep(1000);

// 3. execute command
const params: lc.DocumentSymbolParams = {
Expand All @@ -29,13 +25,28 @@ Mocha.suite('LSP', () => {
},
};

const syms: Array<lc.SymbolInformation> | Array<lc.DocumentSymbol> | null | undefined = await
const syms: Array<lc.DocumentSymbol> | undefined = await
vscode.commands.executeCommand(
'move-analyzer.textDocumentDocumentSymbol', params,
);

assert.ok(syms);
assert.deepStrictEqual(syms[0]?.kind, lc.SymbolKind.Module);
assert.deepStrictEqual(syms[0].name, 'M1');

assert.ok(syms[0].children);
assert.deepStrictEqual(syms[0]?.children[0]?.kind, lc.SymbolKind.Constant);
assert.deepStrictEqual(syms[0]?.children[0].name, 'SOME_CONST');
assert.deepStrictEqual(syms[0]?.children[1]?.kind, lc.SymbolKind.Struct);
assert.deepStrictEqual(syms[0]?.children[1].name, 'SomeOtherStruct');
assert.ok(syms[0].children[1].children);
assert.deepStrictEqual(syms[0]?.children[1]?.children[0]?.kind, lc.SymbolKind.Field);
assert.deepStrictEqual(syms[0]?.children[1]?.children[0]?.name, 'some_field');
assert.deepStrictEqual(syms[0]?.children[1].name, 'SomeOtherStruct');
assert.deepStrictEqual(syms[0]?.children[2]?.kind, lc.SymbolKind.Function);
assert.deepStrictEqual(syms[0]?.children[2].name, 'some_other_struct');
assert.deepStrictEqual(syms[0]?.children[3]?.kind, lc.SymbolKind.Function);
assert.deepStrictEqual(syms[0]?.children[3].name, 'this_is_a_test');
assert.deepStrictEqual(syms[0]?.children[3]?.detail, '["test", "expected_failure"]');
});
});
2 changes: 1 addition & 1 deletion language/move-analyzer/src/bin/move-analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ fn main() {
Ok(result) => {
match result {
Ok(event) => {
eprintln!("send SymbolicatorEvent: {:?}", event);
eprintln!("send analyzer event: {:?}", event);
let notification = Notification::new(lsp_types::notification::TelemetryEvent::METHOD.to_string(), event);
if let Err(err) = context
.connection
Expand Down
41 changes: 22 additions & 19 deletions language/move-analyzer/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2112,6 +2112,27 @@ pub fn on_document_symbol_request(context: &Context, request: &Request, symbols:

let mut children = vec![];

// handle constants
let cloned_const_def = mod_def.constants.clone();
for (sym, const_def_pos) in cloned_const_def {
let const_range = Range {
start: const_def_pos,
end: const_def_pos,
};

children.push(DocumentSymbol {
name: sym.clone().to_string(),
detail: None,
kind: SymbolKind::Constant,
range: const_range,
selection_range: const_range,
children: None,
tags: Some(vec![]),
deprecated: Some(false),
});
}

// handle structs
let cloned_struct_def = mod_def.structs.clone();
for (sym, struct_def) in cloned_struct_def {
let struct_range = Range {
Expand All @@ -2134,25 +2155,7 @@ pub fn on_document_symbol_request(context: &Context, request: &Request, symbols:
});
}

let cloned_const_def = mod_def.constants.clone();
for (sym, const_def_pos) in cloned_const_def {
let const_range = Range {
start: const_def_pos,
end: const_def_pos,
};

children.push(DocumentSymbol {
name: sym.clone().to_string(),
detail: None,
kind: SymbolKind::Constant,
range: const_range,
selection_range: const_range,
children: None,
tags: Some(vec![]),
deprecated: Some(false),
});
}

// handle functions
let cloned_func_def = mod_def.functions.clone();
for (sym, func_def) in cloned_func_def {
let func_range = Range {
Expand Down

0 comments on commit 0be56e1

Please sign in to comment.