public sealed override bool ShouldTriggerCompletion(SourceText text, int caretPosition, CompletionTrigger trigger, OptionSet options) { var lineStart = text.Lines.GetLineFromPosition(caretPosition).Start; // check if the line starts with {whitespace}#{whitespace}{DirectiveName}{whitespace}" var poundIndex = text.IndexOfNonWhiteSpace(lineStart, caretPosition - lineStart); if (poundIndex == -1 || text[poundIndex] != '#') { return(false); } var directiveNameStartIndex = text.IndexOfNonWhiteSpace(poundIndex + 1, caretPosition - poundIndex - 1); if (directiveNameStartIndex == -1 || !text.ContentEquals(directiveNameStartIndex, DirectiveName)) { return(false); } var directiveNameEndIndex = directiveNameStartIndex + DirectiveName.Length; var quoteIndex = text.IndexOfNonWhiteSpace(directiveNameEndIndex, caretPosition - directiveNameEndIndex); if (quoteIndex == -1 || text[quoteIndex] != '"') { return(false); } return(true); }
public static IReadOnlyList <TextChange> GetMinimalTextChanges(SourceText oldText, SourceText newText, bool lineDiffOnly = true) { if (oldText is null) { throw new ArgumentNullException(nameof(oldText)); } if (newText is null) { throw new ArgumentNullException(nameof(newText)); } if (oldText.ContentEquals(newText)) { return(Array.Empty <TextChange>()); } else if (oldText.Length == 0 || newText.Length == 0) { return(newText.GetTextChanges(oldText)); } var differ = new SourceTextDiffer(oldText, newText, lineDiffOnly); var edits = differ.ComputeDiff(); var changes = differ.ProcessChanges(edits); Debug.Assert(oldText.WithChanges(changes).ContentEquals(newText), "Incorrect minimal changes"); return(changes); }
private static TextAndVersion GetProperTextAndVersion(SourceText oldText, SourceText newText, VersionStamp version, string filePath) { // if the supplied text is the same as the previous text, then also use same version // otherwise use new version return(oldText.ContentEquals(newText) ? TextAndVersion.Create(newText, version, filePath) : TextAndVersion.Create(newText, version.GetNewerVersion(), filePath)); }
protected override bool ContentEqualsImpl(SourceText other) { var otherChecksum = other as ChecksumSourceText; if (otherChecksum != null) { return(Checksum == otherChecksum.Checksum); } return(_sourceText.ContentEquals(other)); }
public override void PublishHtml(string filePath, SourceText sourceText, long hostDocumentVersion) { if (filePath is null) { throw new ArgumentNullException(nameof(filePath)); } if (sourceText is null) { throw new ArgumentNullException(nameof(sourceText)); } _foregroundDispatcher.AssertForegroundThread(); if (!_publishedHtmlData.TryGetValue(filePath, out var previouslyPublishedData)) { previouslyPublishedData = PublishData.Default; } IReadOnlyList <TextChange> textChanges = Array.Empty <TextChange>(); if (!sourceText.ContentEquals(previouslyPublishedData.SourceText)) { textChanges = sourceText.GetTextChanges(previouslyPublishedData.SourceText); } else if (hostDocumentVersion == previouslyPublishedData.HostDocumentVersion) { // Source texts match along with host document versions. We've already published something that looks like this. No-op. return; } _publishedHtmlData[filePath] = new PublishData(sourceText, hostDocumentVersion); var request = new UpdateBufferRequest() { HostDocumentFilePath = filePath, Changes = textChanges, HostDocumentVersion = hostDocumentVersion, }; _server.Value.Client.SendRequest(LanguageServerConstants.RazorUpdateHtmlBufferEndpoint, request); }
public static void DoVsCodeHack(string relatedDirectory, string fileName, SourceText sourceText) { try { var filePath = Path.Combine(relatedDirectory, fileName); if (File.Exists(filePath)) { var fileText = File.ReadAllText(filePath); var sourceFileText = SourceText.From(fileText, Encoding.UTF8); if (sourceText.ContentEquals(sourceFileText)) { return; } } using var writer = new StreamWriter(filePath); sourceText.Write(writer); } catch (Exception) { return; // We don't really care if the VSCode hack fails. } }
public override void Publish(string filePath, SourceText sourceText, long hostDocumentVersion) { if (filePath is null) { throw new ArgumentNullException(nameof(filePath)); } if (sourceText is null) { throw new ArgumentNullException(nameof(sourceText)); } _foregroundDispatcher.AssertForegroundThread(); if (!_publishedSourceText.TryGetValue(filePath, out var previouslyPublishedText)) { previouslyPublishedText = EmptySourceText; } IReadOnlyList <TextChange> textChanges = Array.Empty <TextChange>(); if (!sourceText.ContentEquals(previouslyPublishedText)) { textChanges = sourceText.GetTextChanges(previouslyPublishedText); } _publishedSourceText[filePath] = sourceText; var request = new UpdateCSharpBufferRequest() { HostDocumentFilePath = filePath, Changes = textChanges, HostDocumentVersion = hostDocumentVersion, }; _server.Value.Client.SendRequest("updateCSharpBuffer", request); }
protected override bool ContentEqualsImpl(SourceText other) => _sourceText.ContentEquals(other);