Initial support for modules

This commit is contained in:
Deukhoofd 2018-12-14 17:55:09 +01:00
parent 214e2259b2
commit 856a2cacc4
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
8 changed files with 30 additions and 10 deletions

View File

@ -38,6 +38,11 @@
"type": "number", "type": "number",
"default": 100, "default": 100,
"description": "Controls the maximum number of problems produced by the server." "description": "Controls the maximum number of problems produced by the server."
},
"upsilonLanguageServer.moduleDirectory": {
"type": "string",
"default": "./modules",
"description": "The relative path from the workspace where the modules can be found."
} }
} }
} }

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using LanguageServer.VsCode.Contracts; using LanguageServer.VsCode.Contracts;
using LanguageServer.VsCode.Server; using LanguageServer.VsCode.Server;
@ -13,12 +14,11 @@ namespace UpsilonLanguageServer
public DiagnosticProvider() public DiagnosticProvider()
{ {
} }
public static async Task<IEnumerable<Diagnostic>> LintDocument(TextDocument document, SessionDocument session, public static async Task<IEnumerable<Diagnostic>> LintDocument(TextDocument document, SessionDocument session,
int maxNumberOfProblems) int maxNumberOfProblems, string modulePath)
{ {
var diag = new List<Diagnostic>(); var diag = new List<Diagnostic>();
var content = document.Content; var content = document.Content;
@ -30,7 +30,7 @@ namespace UpsilonLanguageServer
return diag; return diag;
} }
var task = RealLint(document, maxNumberOfProblems, session, content, diag); var task = RealLint(document, maxNumberOfProblems, session, content, diag, modulePath);
// either wait until the script is linted, or until a second has passed (timeout) // either wait until the script is linted, or until a second has passed (timeout)
await Task.WhenAny(task, Task.Delay(1000)); await Task.WhenAny(task, Task.Delay(1000));
session.IsChanging = false; session.IsChanging = false;
@ -44,8 +44,9 @@ namespace UpsilonLanguageServer
}; };
private static async Task RealLint(TextDocument document, int maxNumberOfProblems, SessionDocument session, private static async Task RealLint(TextDocument document, int maxNumberOfProblems, SessionDocument session,
string content, List<Diagnostic> diag) string content, ICollection<Diagnostic> diag, string modulePath)
{ {
Options.ScriptLoader.ModulesPath = modulePath;
try try
{ {
using (var script = Executor.ParseInput(content, Options)) using (var script = Executor.ParseInput(content, Options))
@ -55,10 +56,12 @@ namespace UpsilonLanguageServer
foreach (var error in script.Diagnostics.Errors) foreach (var error in script.Diagnostics.Errors)
{ {
diag.Add(ConvertToVsCodeDiagnostic(error)); diag.Add(ConvertToVsCodeDiagnostic(error));
if (diag.Count >= maxNumberOfProblems) break;
} }
foreach (var warning in script.Diagnostics.Warnings) foreach (var warning in script.Diagnostics.Warnings)
{ {
diag.Add(ConvertToVsCodeDiagnostic(warning)); diag.Add(ConvertToVsCodeDiagnostic(warning));
if (diag.Count >= maxNumberOfProblems) break;
} }
session.Bound = bound; session.Bound = bound;

View File

@ -12,11 +12,11 @@ namespace UpsilonLanguageServer.Services
{ {
public class InitializationService : UpsilonLanguageServiceBase public class InitializationService : UpsilonLanguageServiceBase
{ {
[JsonRpcMethod(AllowExtensionData = true)] [JsonRpcMethod(AllowExtensionData = true)]
public InitializeResult Initialize(int processId, Uri rootUri, ClientCapabilities capabilities, public InitializeResult Initialize(int processId, Uri rootUri, ClientCapabilities capabilities,
JToken initializationOptions = null, string trace = null) JToken initializationOptions = null, string trace = null)
{ {
WorkspaceService.RootUri = rootUri?.AbsolutePath;
if (rootUri != null) if (rootUri != null)
{ {
var configPath = rootUri.AbsolutePath + "/.upsilon"; var configPath = rootUri.AbsolutePath + "/.upsilon";
@ -30,6 +30,9 @@ namespace UpsilonLanguageServer.Services
{ {
BoundTypeParser.LoadStaticVariables(File.ReadAllText(staticConfigFile)); BoundTypeParser.LoadStaticVariables(File.ReadAllText(staticConfigFile));
} }
Session.Settings.ModuleDirectory = Path.Combine(rootUri.AbsolutePath, Session.Settings.ModuleDirectory);
Session.Client.Window.LogMessage(MessageType.Error, Session.Settings.ModuleDirectory);
} }
return new InitializeResult(new ServerCapabilities return new InitializeResult(new ServerCapabilities
{ {

View File

@ -118,7 +118,8 @@ namespace UpsilonLanguageServer.Services
// Lint the document when it's changed. // Lint the document when it's changed.
var sessionDocument = ((SessionDocument) sender); var sessionDocument = ((SessionDocument) sender);
var doc1 = sessionDocument.Document; var doc1 = sessionDocument.Document;
var diag1 = DiagnosticProvider.LintDocument(doc1, sessionDocument, session.Settings.MaxNumberOfProblems); var diag1 = DiagnosticProvider.LintDocument(doc1, sessionDocument, session.Settings.MaxNumberOfProblems,
session.Settings.ModuleDirectory);
if (session.Documents.ContainsKey(doc1.Uri)) if (session.Documents.ContainsKey(doc1.Uri))
{ {
// In case the document has been closed when we were linting… // In case the document has been closed when we were linting…
@ -126,7 +127,8 @@ namespace UpsilonLanguageServer.Services
} }
}; };
Session.Documents.TryAdd(textDocument.Uri, doc); Session.Documents.TryAdd(textDocument.Uri, doc);
var diag = DiagnosticProvider.LintDocument(doc.Document, doc, Session.Settings.MaxNumberOfProblems); var diag = DiagnosticProvider.LintDocument(doc.Document, doc, Session.Settings.MaxNumberOfProblems,
session.Settings.ModuleDirectory);
await Client.Document.PublishDiagnostics(textDocument.Uri, await diag); await Client.Document.PublishDiagnostics(textDocument.Uri, await diag);
} }

View File

@ -11,13 +11,17 @@ namespace UpsilonLanguageServer.Services
[JsonRpcScope(MethodPrefix = "workspace/")] [JsonRpcScope(MethodPrefix = "workspace/")]
public class WorkspaceService : UpsilonLanguageServiceBase public class WorkspaceService : UpsilonLanguageServiceBase
{ {
public static string RootUri { get; set; }
[JsonRpcMethod(IsNotification = true)] [JsonRpcMethod(IsNotification = true)]
public async Task DidChangeConfiguration(SettingsRoot settings) public async Task DidChangeConfiguration(SettingsRoot settings)
{ {
Session.Settings = settings.UpsilonLanguageServer; Session.Settings = settings.UpsilonLanguageServer;
Session.Settings.ModuleDirectory = Path.Combine(RootUri, Session.Settings.ModuleDirectory);
foreach (var doc in Session.Documents.Values) foreach (var doc in Session.Documents.Values)
{ {
var diag = await DiagnosticProvider.LintDocument(doc.Document, doc, Session.Settings.MaxNumberOfProblems); var diag = await DiagnosticProvider.LintDocument(doc.Document, doc, Session.Settings.MaxNumberOfProblems,
Session.Settings.ModuleDirectory);
await Client.Document.PublishDiagnostics(doc.Document.Uri, diag); await Client.Document.PublishDiagnostics(doc.Document.Uri, diag);
} }
} }
@ -59,7 +63,8 @@ namespace UpsilonLanguageServer.Services
BoundTypeParser.LoadBoundTypes(File.ReadAllText(localPath)); BoundTypeParser.LoadBoundTypes(File.ReadAllText(localPath));
foreach (var doc in Session.Documents.Values) foreach (var doc in Session.Documents.Values)
{ {
var diag = await DiagnosticProvider.LintDocument(doc.Document, doc, Session.Settings.MaxNumberOfProblems); var diag = await DiagnosticProvider.LintDocument(doc.Document, doc,
Session.Settings.MaxNumberOfProblems, Session.Settings.ModuleDirectory);
await Client.Document.PublishDiagnostics(doc.Document.Uri, diag); await Client.Document.PublishDiagnostics(doc.Document.Uri, diag);
} }
} }
@ -72,7 +77,8 @@ namespace UpsilonLanguageServer.Services
BoundTypeParser.LoadStaticVariables(File.ReadAllText(localPath)); BoundTypeParser.LoadStaticVariables(File.ReadAllText(localPath));
foreach (var doc in Session.Documents.Values) foreach (var doc in Session.Documents.Values)
{ {
var diag = await DiagnosticProvider.LintDocument(doc.Document, doc, Session.Settings.MaxNumberOfProblems); var diag = await DiagnosticProvider.LintDocument(doc.Document, doc,
Session.Settings.MaxNumberOfProblems, Session.Settings.ModuleDirectory);
await Client.Document.PublishDiagnostics(doc.Document.Uri, diag); await Client.Document.PublishDiagnostics(doc.Document.Uri, diag);
} }
} }

View File

@ -8,6 +8,7 @@ namespace UpsilonLanguageServer
public class LanguageServerSettings public class LanguageServerSettings
{ {
public int MaxNumberOfProblems { get; set; } = 10; public int MaxNumberOfProblems { get; set; } = 10;
public string ModuleDirectory { get; set; } = "./modules";
public LanguageServerTraceSettings Trace { get; } = new LanguageServerTraceSettings(); public LanguageServerTraceSettings Trace { get; } = new LanguageServerTraceSettings();
} }