using System.Collections.Generic; using System.Collections.Immutable; using System.IO; using System.Linq; using OmniSharp.Extensions.LanguageServer.Protocol; using OmniSharp.Extensions.LanguageServer.Protocol.Models; using Range = OmniSharp.Extensions.LanguageServer.Protocol.Models.Range; namespace PortingAssistantExtensionServer.TextDocumentModels { class CodeFileDocument { private string _content; private bool _isParsed; private ImmutableArray _sections = ImmutableArray.Empty; private ImmutableDictionary _values = ImmutableDictionary.Empty; private ImmutableArray _diagnostics; private int _version = 1; private readonly DocumentUri documentUri; public CodeFileDocument(DocumentUri documentUri) { this.documentUri = documentUri; } public void Load(string content) { _content = content; _isParsed = false; _version++; } public string NormalizedPath => documentUri.Path.StartsWith("/") ? documentUri.Path.Substring(1) : documentUri.Path; public string GetProjectFile() { var currentDir = Directory.GetParent(this.NormalizedPath)?.FullName; var projectFile = ""; while (!string.IsNullOrEmpty(currentDir)) { projectFile = Directory.EnumerateFiles(currentDir, "*.csproj").FirstOrDefault() ?? Directory.EnumerateFiles(currentDir, "*.vbproj").FirstOrDefault(); if (projectFile != null) { return projectFile; } else { currentDir = Directory.GetParent(currentDir)?.FullName; } } return projectFile; } public int Version => _version; public DocumentUri DocumentUri => documentUri; public string GetText() => _content; public IEnumerable GetSections() { EnsureParsed(); return _sections; } public IEnumerable GetKeys() { EnsureParsed(); return _values.Keys; } private void EnsureParsed() { if (_isParsed) return; var values = ImmutableDictionary.Empty.ToBuilder(); var diagnostics = ImmutableArray.Empty.ToBuilder(); var sections = ImmutableArray.Empty.ToBuilder(); //TODO parse code actions to diagnostics _diagnostics = diagnostics.ToImmutable(); _values = values.ToImmutable(); _sections = sections.ToImmutable(); _isParsed = true; } } public class CsValue { public string Section; public string Key; public string Value; public Range KeyLocation; public Range ValueLocation; public CsValue(string Section, string Key, Range KeyLocation, string Value, Range ValueLocation) { this.Section = Section; this.Key = Key; this.Value = Value; this.KeyLocation = KeyLocation; this.ValueLocation = ValueLocation; } } public class CsSection { public string Section; public Range Location; public CsSection(string Section, Range Location) { this.Section = Section; this.Location = Location; } } }