platform/jael_language_server/lib/src/server.dart

420 lines
12 KiB
Dart
Raw Normal View History

2018-11-12 18:20:39 +00:00
import 'dart:async';
import 'package:dart_language_server/src/protocol/language_server/interface.dart';
import 'package:dart_language_server/src/protocol/language_server/messages.dart';
import 'package:file/file.dart';
import 'package:file/local.dart';
import 'package:file/memory.dart';
2018-11-12 20:50:52 +00:00
import 'package:jael/jael.dart';
2018-11-12 18:20:39 +00:00
import 'package:logging/logging.dart';
import 'package:path/path.dart' as p;
2018-11-12 20:50:52 +00:00
import 'package:source_span/source_span.dart';
import 'package:symbol_table/symbol_table.dart';
import 'analyzer.dart';
import 'object.dart';
2018-11-12 18:20:39 +00:00
class JaelLanguageServer extends LanguageServer {
2018-11-12 20:50:52 +00:00
var _diagnostics = new StreamController<Diagnostics>(sync: true);
2018-11-12 18:20:39 +00:00
var _done = new Completer();
var _memFs = new MemoryFileSystem();
var _localFs = const LocalFileSystem();
Directory _localRootDir;
2018-11-12 20:50:52 +00:00
var logger = new Logger('jael');
Uri _rootUri;
2018-11-12 18:20:39 +00:00
var _workspaceEdits = new StreamController<ApplyWorkspaceEditParams>();
@override
Stream<Diagnostics> get diagnostics => _diagnostics.stream;
@override
Future<void> get onDone => _done.future;
@override
Stream<ApplyWorkspaceEditParams> get workspaceEdits => _workspaceEdits.stream;
@override
Future<void> shutdown() {
if (!_done.isCompleted) _done.complete();
_diagnostics.close();
_workspaceEdits.close();
return super.shutdown();
}
@override
Future<ServerCapabilities> initialize(int clientPid, String rootUri,
ClientCapabilities clientCapabilities, String trace) async {
// Find our real root dir.
2018-11-12 20:50:52 +00:00
_localRootDir = _localFs.directory(_rootUri = Uri.parse(rootUri));
2018-11-12 18:20:39 +00:00
// Copy all real files that end in *.jael (and *.jl for legacy) into the in-memory filesystem.
await for (var entity in _localRootDir.list(recursive: true)) {
if (entity is File && p.extension(entity.path) == '.jael') {
var relativePath =
p.relative(entity.absolute.path, from: _localRootDir.absolute.path);
var file = _memFs.file(relativePath);
await file.create(recursive: true);
await entity.openRead().pipe(file.openWrite(mode: FileMode.write));
2018-11-12 20:50:52 +00:00
logger.info('Found Jael file ${file.path}');
// Analyze it
var documentId = new TextDocumentIdentifier((b) {
b..uri = _rootUri.replace(path: relativePath).toString();
});
await analyzerForId(documentId);
2018-11-12 18:20:39 +00:00
}
}
return new ServerCapabilities((b) {
b
2018-11-12 20:50:52 +00:00
..codeActionProvider = false
..completionProvider = new CompletionOptions((b) {
b
..resolveProvider = true
..triggerCharacters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdeghijklmnopqrstuvxwyz'
.codeUnits
.map((c) => new String.fromCharCode(c))
.toList();
})
..definitionProvider = true
..documentHighlightProvider = true
..documentRangeFormattingProvider = false
..documentOnTypeFormattingProvider = null
2018-11-12 18:20:39 +00:00
..documentSymbolProvider = true
..documentFormattingProvider = true
..hoverProvider = true
..implementationProvider = true
..referencesProvider = true
..renameProvider = true
..signatureHelpProvider = new SignatureHelpOptions((b) {})
..textDocumentSync = new TextDocumentSyncOptions((b) {
b
2018-11-12 20:50:52 +00:00
..openClose = true
2018-11-12 18:20:39 +00:00
..change = TextDocumentSyncKind.incremental
..save = new SaveOptions((b) {
2018-11-12 20:50:52 +00:00
b..includeText = false;
2018-11-12 18:20:39 +00:00
})
2018-11-12 20:50:52 +00:00
..willSave = false
..willSaveWaitUntil = false;
})
..workspaceSymbolProvider = true;
});
}
Future<File> fileForId(TextDocumentIdentifier documentId) async {
var uri = Uri.parse(documentId.uri);
var relativePath = p.relative(uri.path, from: _rootUri.path);
var file = _memFs.file(relativePath);
if (!await file.exists()) {
await file.create(recursive: true);
await _localFs.file(uri).openRead().pipe(file.openWrite());
logger.info('Opened Jael file ${file.path}');
}
return file;
}
Future<Scanner> scannerForId(TextDocumentIdentifier documentId) async {
var file = await fileForId(documentId);
return scan(await file.readAsString(), sourceUrl: file.uri);
}
Future<Analyzer> analyzerForId(TextDocumentIdentifier documentId) async {
var scanner = await scannerForId(documentId);
var analyzer = new Analyzer(scanner, logger)..errors.addAll(scanner.errors);
analyzer.parseDocument();
logger.info(
'Done ${documentId.uri} ${await (await fileForId(documentId)).readAsString()}');
logger.info(analyzer.errors);
emitDiagnostics(documentId.uri, analyzer.errors.map(toDiagnostic).toList());
return analyzer;
}
Diagnostic toDiagnostic(JaelError e) {
return new Diagnostic((b) {
b
..message = e.message
..range = toRange(e.span)
..severity = toSeverity(e.severity)
..source = e.span.start.sourceUrl.toString();
});
}
int toSeverity(JaelErrorSeverity s) {
switch (s) {
case JaelErrorSeverity.warning:
return DiagnosticSeverity.warning;
default:
return DiagnosticSeverity.error;
}
}
Range toRange(FileSpan span) {
return new Range((b) {
b
..start = toPosition(span.start)
..end = toPosition(span.end);
});
}
Range emptyRange() {
return new Range((b) => b
..start = b.end = new Position((b) {
b
..character = 1
..line = 0;
}));
}
Position toPosition(SourceLocation location) {
return new Position((b) {
b
..line = location.line
..character = location.column;
});
}
Location toLocation(String uri, FileSpan span) {
return new Location((b) {
b
..range = toRange(span)
..uri = uri;
2018-11-12 18:20:39 +00:00
});
}
2018-11-12 20:50:52 +00:00
bool isReachable(JaelObject obj, Position position) {
return obj.span.start.line <= position.line &&
obj.span.start.column <= position.character;
}
CompletionItem toCompletion(Variable<JaelObject> symbol) {
var value = symbol.value;
if (value is JaelCustomElement) {
var name = value.name;
return new CompletionItem((b) {
b
..kind = CompletionItemKind.classKind
..label = symbol.name
..textEdit = new TextEdit((b) {
b
..range = emptyRange()
..newText = '<$name\$1>\n \$2\n</name>';
});
});
}
return null;
}
void emitDiagnostics(String uri, Iterable<Diagnostic> diagnostics) {
if (diagnostics.isEmpty) return;
_diagnostics.add(new Diagnostics((b) {
b
..diagnostics = diagnostics.toList()
..uri = uri.toString();
}));
}
@override
Future textDocumentDidOpen(TextDocumentItem document) async {
await analyzerForId(
new TextDocumentIdentifier((b) => b..uri = document.uri));
}
@override
Future textDocumentDidChange(VersionedTextDocumentIdentifier documentId,
List<TextDocumentContentChangeEvent> changes) async {
var id = new TextDocumentIdentifier((b) => b..uri = documentId.uri);
var file = await fileForId(id);
for (var change in changes) {
if (change.range != null) {
await file.writeAsString(change.text);
} else {
var contents = await file.readAsString();
int findIndex(Position position) {
var lines = contents.split('\n');
// Sum the length of the previous lines.
int lineLength = lines
.take(position.line - 1)
.map((s) => s.length)
.reduce((a, b) => a + b);
return lineLength + position.character - 1;
}
if (change.range == null) {
contents = change.text;
} else {
var start = findIndex(change.range.start),
end = findIndex(change.range.end);
contents = contents.replaceRange(start, end, change.text);
}
await file.writeAsString(contents);
}
}
await analyzerForId(id);
}
2018-11-12 18:20:39 +00:00
@override
Future<List> textDocumentCodeAction(TextDocumentIdentifier documentId,
2018-11-12 20:50:52 +00:00
Range range, CodeActionContext context) async {
2018-11-12 18:20:39 +00:00
// TODO: implement textDocumentCodeAction
2018-11-12 20:50:52 +00:00
return [];
2018-11-12 18:20:39 +00:00
}
@override
Future<CompletionList> textDocumentCompletion(
2018-11-12 20:50:52 +00:00
TextDocumentIdentifier documentId, Position position) async {
var analyzer = await analyzerForId(documentId);
var symbols = analyzer.scope.allVariables;
var reachable = symbols.where((s) => isReachable(s.value, position));
return new CompletionList((b) {
b
..isIncomplete = false
..items = reachable.map(toCompletion).toList();
});
}
Future<JaelObject> currentSymbol(
TextDocumentIdentifier documentId, Position position) async {
var analyzer = await analyzerForId(documentId);
var symbols = analyzer.allDefinitions; // analyzer.scope.allVariables;
logger.info('Current synmbols: ${symbols.map((v) => v.name)}');
for (var s in symbols) {
var v = s.value;
if (position.line == v.span.start.line &&
position.character == v.span.start.column) {
logger.info('Success ${s.name}');
return v;
} else {
logger.info(
'Nope ${s.name} (${v.span.start.toolString} vs ${position.line}:${position.character})');
}
}
return null;
2018-11-12 18:20:39 +00:00
}
@override
Future<Location> textDocumentDefinition(
2018-11-12 20:50:52 +00:00
TextDocumentIdentifier documentId, Position position) async {
var symbol = await currentSymbol(documentId, position);
if (symbol != null) {
return toLocation(documentId.uri, symbol.span);
}
return null;
2018-11-12 18:20:39 +00:00
}
@override
Future<List<DocumentHighlight>> textDocumentHighlight(
2018-11-12 20:50:52 +00:00
TextDocumentIdentifier documentId, Position position) async {
var symbol = await currentSymbol(documentId, position);
if (symbol != null) {
return symbol.usages.map((u) {
return new DocumentHighlight((b) {
b
..range = toRange(u.span)
..kind = u.type == SymbolUsageType.definition
? DocumentHighlightKind.write
: DocumentHighlightKind.read;
});
}).toList();
}
return [];
2018-11-12 18:20:39 +00:00
}
@override
2018-11-12 20:50:52 +00:00
Future<Hover> textDocumentHover(
TextDocumentIdentifier documentId, Position position) async {
var symbol = await currentSymbol(documentId, position);
if (symbol != null) {
return new Hover((b) {
b..range = toRange(symbol.span);
});
}
return null;
2018-11-12 18:20:39 +00:00
}
@override
Future<List<Location>> textDocumentImplementation(
2018-11-12 20:50:52 +00:00
TextDocumentIdentifier documentId, Position position) async {
var defn = await textDocumentDefinition(documentId, position);
return defn == null ? [] : [defn];
2018-11-12 18:20:39 +00:00
}
@override
Future<List<Location>> textDocumentReferences(
TextDocumentIdentifier documentId,
Position position,
2018-11-12 20:50:52 +00:00
ReferenceContext context) async {
var symbol = await currentSymbol(documentId, position);
if (symbol != null) {
return symbol.usages.map((u) {
return toLocation(documentId.uri, u.span);
}).toList();
}
return [];
2018-11-12 18:20:39 +00:00
}
@override
2018-11-12 20:50:52 +00:00
Future<WorkspaceEdit> textDocumentRename(TextDocumentIdentifier documentId,
Position position, String newName) async {
var symbol = await currentSymbol(documentId, position);
if (symbol != null) {
return new WorkspaceEdit((b) {
b
..changes = {
symbol.name: symbol.usages.map((u) {
return new TextEdit((b) {
b
..range = toRange(u.span)
..newText = newName;
});
}).toList()
};
});
}
return new WorkspaceEdit((b) {
b..changes = {};
});
2018-11-12 18:20:39 +00:00
}
@override
Future<List<SymbolInformation>> textDocumentSymbols(
2018-11-12 20:50:52 +00:00
TextDocumentIdentifier documentId) async {
var analyzer = await analyzerForId(documentId);
return analyzer.allDefinitions.map((symbol) {
return new SymbolInformation((b) {
b
..kind = SymbolKind.classSymbol
..name = symbol.name
..location = toLocation(documentId.uri, symbol.value.span);
});
}).toList();
2018-11-12 18:20:39 +00:00
}
@override
2018-11-12 20:50:52 +00:00
Future<void> workspaceExecuteCommand(String command, List arguments) async {
2018-11-12 18:20:39 +00:00
// TODO: implement workspaceExecuteCommand
}
@override
2018-11-12 20:50:52 +00:00
Future<List<SymbolInformation>> workspaceSymbol(String query) async {
2018-11-12 18:20:39 +00:00
// TODO: implement workspaceSymbol
2018-11-12 20:50:52 +00:00
return [];
2018-11-12 18:20:39 +00:00
}
}
2018-11-12 20:50:52 +00:00
abstract class DiagnosticSeverity {
static const int error = 0, warning = 1, information = 2, hint = 3;
}