Skip to content

Commit

Permalink
Merge pull request #95 from retailcoder/pipeline/state
Browse files Browse the repository at this point in the history
Collects document state, resolves member symbols (some cut corners, but mostly works)
  • Loading branch information
retailcoder authored Feb 26, 2024
2 parents 14de0ab + 804b948 commit eb372a5
Show file tree
Hide file tree
Showing 71 changed files with 1,163 additions and 434 deletions.
1 change: 1 addition & 0 deletions Client/Rubberduck.Unmanaged/Rubberduck.Unmanaged.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<Nullable>disable</Nullable>
<Platforms>AnyCPU</Platforms>
<PlatformTarget>AnyCPU</PlatformTarget>
<EmbedManifest>True</EmbedManifest>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
using Rubberduck.InternalApi.Extensions;
using Rubberduck.InternalApi.ServerPlatform.LanguageServer;
using Rubberduck.InternalApi.Services;
using Rubberduck.ServerPlatform;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
Expand All @@ -12,32 +16,44 @@ namespace Rubberduck.LanguageServer.Handlers.Language
{
public class DocumentSymbolHandler : DocumentSymbolHandlerBase
{
private readonly ILanguageServerFacade _server;
private readonly SupportedLanguage _language;
private readonly DocumentContentStore _contentStore;

private readonly ServerPlatformServiceHelper _service;
private readonly IWorkspaceStateManager _workspaces;
private readonly TextDocumentSelector _selector;

public DocumentSymbolHandler(ILanguageServerFacade server, SupportedLanguage language, DocumentContentStore contentStore)
public DocumentSymbolHandler(ServerPlatformServiceHelper service, IWorkspaceStateManager workspaces, SupportedLanguage language)
{
_language = language;
_server = server;
_contentStore = contentStore;

var filter = new TextDocumentFilter
{
Language = language.Id,
Pattern = string.Join(";", language.FileTypes.Select(fileType => $"**/{fileType}").ToArray())
};
_selector = new TextDocumentSelector(filter);
_service = service;
_workspaces = workspaces;
_selector = language.ToTextDocumentSelector();
}

public async override Task<SymbolInformationOrDocumentSymbolContainer?> Handle(DocumentSymbolParams request, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

var items = new List<SymbolInformationOrDocumentSymbol>();
// TODO

if (!_service.TryRunAction(() =>
{
var documentUri = request.TextDocument.Uri;
var workspace = _workspaces.ActiveWorkspace
?? throw new InvalidOperationException("Invalid WorkspaceStateManager state: there is no active workspace.");

var uri = new WorkspaceFileUri(documentUri.ToUri().OriginalString, workspace.WorkspaceRoot!.WorkspaceRoot);
if (workspace.TryGetWorkspaceFile(uri, out var document) && document?.Symbol != null)
{
items.Add(new SymbolInformationOrDocumentSymbol(document.Symbol));
_service.LogInformation($"Found symbol ({document.Symbol.GetType().Name}: {document.Symbol.Children!.Count()} children) for document at uri '{uri}'.");
}
else
{
_service.LogWarning($"Could not find workspace file at uri '{uri}'. An empty collection will be returned.");
}
}, out var exception, nameof(DocumentSymbolHandler)) && exception != null)
{
// in case of failure, we throw here to return an error response:
throw exception;
}

var result = new SymbolInformationOrDocumentSymbolContainer(items);
return await Task.FromResult(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
using Rubberduck.InternalApi.Extensions;
using Rubberduck.InternalApi.ServerPlatform.LanguageServer;
using Rubberduck.InternalApi.Services;
using Rubberduck.ServerPlatform;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
Expand All @@ -12,32 +16,43 @@ namespace Rubberduck.LanguageServer.Handlers.Language
{
public class FoldingRangeHandler : FoldingRangeHandlerBase
{
private readonly ILanguageServerFacade _server;
private readonly SupportedLanguage _language;
private readonly DocumentContentStore _contentStore;

private readonly ServerPlatformServiceHelper _service;
private readonly IWorkspaceStateManager _workspaces;
private readonly TextDocumentSelector _selector;

public FoldingRangeHandler(ILanguageServerFacade server, SupportedLanguage language, DocumentContentStore contentStore)
public FoldingRangeHandler(ServerPlatformServiceHelper service, IWorkspaceStateManager workspaces, SupportedLanguage language)
{
_language = language;
_server = server;
_contentStore = contentStore;

var filter = new TextDocumentFilter
{
Language = language.Id,
Pattern = string.Join(";", language.FileTypes.Select(fileType => $"**/{fileType}").ToArray())
};
_selector = new TextDocumentSelector(filter);
_service = service;
_workspaces = workspaces;
_selector = language.ToTextDocumentSelector();
}

public async override Task<Container<FoldingRange>?> Handle(FoldingRangeRequestParam request, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

var items = new List<FoldingRange>();
// TODO

if (!_service.TryRunAction(() =>
{
var documentUri = request.TextDocument.Uri;
var workspace = _workspaces.ActiveWorkspace
?? throw new InvalidOperationException("Invalid WorkspaceStateManager state: there is no active workspace.");

var uri = new WorkspaceFileUri(documentUri.ToUri().OriginalString, workspace.WorkspaceRoot!.WorkspaceRoot);
if (workspace.TryGetWorkspaceFile(uri, out var document) && document != null)
{
items.AddRange(document.Foldings);
_service.LogInformation($"Found {document.Foldings.Count} foldings for document at uri '{uri}'.");
}
else
{
_service.LogWarning($"Could not find workspace file at uri '{uri}'. An empty collection will be returned.");
}
}, out var exception, nameof(FoldingRangeHandler)) && exception != null)
{
// in case of failure, we throw here to return an error response:
throw exception;
}

var result = new Container<FoldingRange>(items);
return await Task.FromResult(result);
Expand Down
2 changes: 2 additions & 0 deletions Server/Rubberduck.LanguageServer/LanguageServerState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Rubberduck.LanguageServer
{


public interface ILanguageServerState : IServerStateWriter
{
DocumentUri? RootUri { get; set; }
Expand Down
44 changes: 19 additions & 25 deletions Server/Rubberduck.LanguageServer/ServerApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@
using OmniSharp.Extensions.LanguageServer.Protocol.Server.WorkDone;
using OmniSharp.Extensions.LanguageServer.Server;
using Rubberduck.InternalApi.Extensions;
using Rubberduck.InternalApi.Model.Declarations.Execution;
using Rubberduck.InternalApi.ServerPlatform.LanguageServer;
using Rubberduck.InternalApi.Services;
using Rubberduck.InternalApi.Settings;
using Rubberduck.InternalApi.Settings.Model;
using Rubberduck.InternalApi.Settings.Model.LanguageClient;
using Rubberduck.InternalApi.Settings.Model.LanguageServer;
using Rubberduck.LanguageServer.Handlers.Language;
using Rubberduck.LanguageServer.Handlers.Lifecycle;
using Rubberduck.LanguageServer.Handlers.Workspace;
using Rubberduck.Parsing._v3.Pipeline;
using Rubberduck.Parsing._v3.Pipeline.Services;
using Rubberduck.Parsing.Abstract;
using Rubberduck.Parsing.COM.Abstract;
using Rubberduck.Parsing.Exceptions;
using Rubberduck.Parsing.Model.ComReflection;
using Rubberduck.Parsing.Parsers;
using Rubberduck.Parsing.PreProcessing;
using Rubberduck.Parsing.TokenStreamProviders;
Expand Down Expand Up @@ -58,38 +63,19 @@ public async override Task TestConfigAsync()

await task;
logger.LogInformation($"Workspace was processed: {task.Status}");

var workspace = app.State.ActiveWorkspace!;
logger.LogInformation($"{workspace.ExecutionContext.UnresolvedSymbols.Count} unresolved symbols.");
}
}

//private TextDocumentSelector GetSelector(SupportedLanguage language)
//{
// var filter = new TextDocumentFilter
// {
// Language = language.Id,
// Pattern = string.Join(";", language.FileTypes.Select(fileType => $"**/{fileType}").ToArray())
// };
// return new TextDocumentSelector(filter);
//}

//private void HandleDidOpenTextDocument(DidOpenTextDocumentParams request, TextSynchronizationCapability capability, CancellationToken token)
//{
// _logger?.LogDebug("Received DidOpenTextDocument notification.");
//}

//private TextDocumentOpenRegistrationOptions GetTextDocumentOpenRegistrationOptions(TextSynchronizationCapability capability, ClientCapabilities clientCapabilities)
//{
// return new TextDocumentOpenRegistrationOptions
// {
// DocumentSelector = GetSelector(new VisualBasicForApplicationsLanguage())
// };
//}
await Task.Delay(100);
}

protected override ServerState<LanguageServerSettings, LanguageServerStartupSettings> GetServerState(IServiceProvider provider)
=> provider.GetRequiredService<LanguageServerState>();

protected override void ConfigureServices(ServerStartupOptions options, IServiceCollection services)
{
//services.AddSingleton<ILanguageServerFacade>(provider => _languageServer);
services.AddSingleton<ServerStartupOptions>(provider => options);

if (options.ClientProcessId > 0)
Expand All @@ -107,8 +93,10 @@ protected override void ConfigureServices(ServerStartupOptions options, IService

services.AddSingleton<IWorkspaceService, WorkspaceService>();
services.AddSingleton<IWorkspaceStateManager, WorkspaceStateManager>();

services.AddSingleton<ISyntaxErrorMessageService, SyntaxErrorMessageService>();
services.AddSingleton<WorkspacePipeline>();
services.AddSingleton<LibrarySymbolsService>();
services.AddSingleton<IComLibraryProvider, ComLibraryProvider>();
services.AddSingleton<ParserPipelineSectionProvider>();
services.AddTransient<WorkspaceDocumentParserOrchestrator>();
services.AddTransient<DocumentParserSection>();
Expand Down Expand Up @@ -182,8 +170,10 @@ protected override void ConfigureHandlers(LanguageServerOptions options)
.WithHandler<DocumentHighlightHandler>()
.WithHandler<DocumentOnTypeFormattingHandler>()
.WithHandler<DocumentRangeFormattingHandler>()
*/
.WithHandler<DocumentSymbolHandler>()
.WithHandler<FoldingRangeHandler>()
/*
.WithHandler<HoverHandler>()
.WithHandler<ImplementationHandler>()
.WithHandler<PrepareRenameHandler>()
Expand Down Expand Up @@ -224,6 +214,10 @@ protected async override Task OnServerStartedAsync(ILanguageServer server, Cance

service.LogInformation($"Project {pipeline.State.ProjectName} ({pipeline.State.WorkspaceFiles.Count()} files) is good to go!");
}
else
{
service.LogWarning("Workspace was not loaded.", "IWorkspaceService.OpenProjectWorkspaceAsync(Uri) returned false.");
}
}
}
}
23 changes: 23 additions & 0 deletions Server/Rubberduck.LanguageServer/SyntaxErrorMessageService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.Extensions.Logging;
using Rubberduck.InternalApi.Services;
using Rubberduck.InternalApi.Settings;
using Rubberduck.Parsing.Exceptions;

namespace Rubberduck.LanguageServer
{
public class SyntaxErrorMessageService : ServiceBase, ISyntaxErrorMessageService
{
public SyntaxErrorMessageService(ILogger<SyntaxErrorMessageService> logger, RubberduckSettingsProvider settingsProvider, PerformanceRecordAggregator performance)
: base(logger, settingsProvider, performance)
{
}

public bool TryGetMeaningfulMessage(AntlrSyntaxErrorInfo info, out string message)
{
/*TODO*/

message = info.Message;
return false;
}
}
}
10 changes: 6 additions & 4 deletions Server/Rubberduck.Parsing/Abstract/ICompilationArgumentsCache.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace Rubberduck.Parsing.Abstract;
using Rubberduck.InternalApi.Extensions;

namespace Rubberduck.Parsing.Abstract;

public interface ICompilationArgumentsCache : ICompilationArgumentsProvider
{
void ReloadCompilationArguments(IEnumerable<Uri> workspaceUris);
IReadOnlyCollection<Uri> ProjectWhoseCompilationArgumentsChanged();
void ReloadCompilationArguments(IEnumerable<WorkspaceUri> workspaceUris);
IReadOnlyCollection<WorkspaceUri> ProjectWhoseCompilationArgumentsChanged();
void ClearProjectWhoseCompilationArgumentsChanged();
void RemoveCompilationArgumentsFromCache(IEnumerable<Uri> workspaceUris);
void RemoveCompilationArgumentsFromCache(IEnumerable<WorkspaceUri> workspaceUris);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Rubberduck.Parsing.PreProcessing;
using Rubberduck.InternalApi.Extensions;
using Rubberduck.Parsing.PreProcessing;

namespace Rubberduck.Parsing.Abstract;

public interface ICompilationArgumentsProvider
{
VBAPredefinedCompilationConstants PredefinedCompilationConstants { get; }
Dictionary<string, short> UserDefinedCompilationArguments(Uri workspaceRoot);
Dictionary<string, short> UserDefinedCompilationArguments(WorkspaceUri workspaceRoot);
}
3 changes: 2 additions & 1 deletion Server/Rubberduck.Parsing/Abstract/IParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ public class ParseResult
public ITokenStream TokenStream { get; init; } = null!;
public LogicalLineStore? LogicalLines { get; init; } = null;
public IEnumerable<IParseTreeListener> Listeners { get; init; } = [];
public IEnumerable<SyntaxErrorInfo> SyntaxErrors { get; init; } = [];
}

public interface IParser<TContent>
{
ParseResult Parse(WorkspaceFileUri uri, TContent content, CancellationToken token, CodeKind codeKind = CodeKind.RubberduckEditorModule, ParserMode parserMode = ParserMode.FallBackSllToLl, IEnumerable<IParseTreeListener>? parseListeners = null);
ParseResult Parse(WorkspaceFileUri uri, TContent content, CancellationToken token, ParserMode parserMode = ParserMode.FallBackSllToLl, IEnumerable<IParseTreeListener>? parseListeners = null);
}

public interface IStringParser : IParser<string> { }
Expand Down
2 changes: 1 addition & 1 deletion Server/Rubberduck.Parsing/Abstract/ITokenStreamParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ namespace Rubberduck.Parsing.Abstract;

public interface ITokenStreamParser
{
IParseTree Parse(WorkspaceFileUri uri, CommonTokenStream tokenStream, CancellationToken token, CodeKind codeKind = CodeKind.RubberduckEditorModule, ParserMode parserMode = ParserMode.FallBackSllToLl, IEnumerable<IParseTreeListener>? parseListeners = null);
IParseTree Parse(WorkspaceFileUri uri, CommonTokenStream tokenStream, CancellationToken token, out IEnumerable<SyntaxErrorInfo> errors, ParserMode parserMode = ParserMode.FallBackSllToLl, IEnumerable<IParseTreeListener>? parseListeners = null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ namespace Rubberduck.Parsing.Abstract;

public interface ITokenStreamPreprocessor
{
CommonTokenStream? PreprocessTokenStream(WorkspaceFileUri uri, CommonTokenStream tokenStream, CancellationToken token, CodeKind codeKind = CodeKind.SnippetCode);
CommonTokenStream? PreprocessTokenStream(WorkspaceFileUri uri, CommonTokenStream tokenStream, CancellationToken token);
}
Loading

0 comments on commit eb372a5

Please sign in to comment.