public ProjectBlockCompletionContext(VsProjectAnalyzer analyzer, ITextBuffer buffer) : base(analyzer, buffer.GetFileName()) { var doc = HtmlEditorDocument.TryFromTextBuffer(buffer); if (doc == null) { return; } var artifacts = doc.HtmlEditorTree.ArtifactCollection; foreach (var artifact in artifacts.OfType <TemplateBlockArtifact>()) { var artifactText = doc.HtmlEditorTree.ParseTree.Text.GetText(artifact.InnerRange.Start, artifact.InnerRange.Length); artifact.Parse(artifactText); if (artifact.Block != null) { var varNames = artifact.Block.GetVariables(); foreach (var varName in varNames) { AddLoopVariable(varName); } } } }
public ITagger <T> CreateTagger <T>(ITextView textView, ITextBuffer buffer) where T : ITag { HtmlEditorDocument document = HtmlEditorDocument.TryFromTextBuffer(buffer); if (document == null) { return(null); } return(new RadioButtonGroupNameReferenceTagger(textView, buffer, document) as ITagger <T>); }
internal static HtmlEditorDocument HtmlEditorDocumentFromTextBuffer(ITextBuffer buffer) { var doc = HtmlEditorDocument.FromTextBuffer(buffer); if (doc == null) { var projBuffer = buffer as IProjectionBuffer; if (projBuffer != null) { foreach (var b in projBuffer.SourceBuffers) { if (b.ContentType.IsOfType(TemplateHtmlContentType.ContentTypeName) && (doc = HtmlEditorDocument.TryFromTextBuffer(b)) != null) { return doc; } } } } return doc; }
protected override IEnumerable <DjangoBlock> GetBlocks(IEnumerable <CompletionInfo> results, SnapshotPoint triggerPoint) { var buffers = _buffer.GetContributingBuffers().Where(b => b.ContentType.IsOfType(TemplateHtmlContentType.ContentTypeName)); var doc = HtmlEditorDocument.TryFromTextBuffer(buffers.FirstOrDefault() ?? _buffer); if (doc == null) { yield break; } var artifacts = doc.HtmlEditorTree.ArtifactCollection.ItemsInRange(new TextRange(0, triggerPoint.Position)); foreach (var artifact in artifacts.OfType <TemplateBlockArtifact>().Reverse()) { var artifactText = doc.HtmlEditorTree.ParseTree.Text.GetText(artifact.InnerRange.Start, artifact.InnerRange.Length); artifact.Parse(artifactText); if (artifact.Block != null) { yield return(artifact.Block); } } }
public override IList <ClassificationSpan> GetClassificationSpans(SnapshotSpan span) { var spans = new List <ClassificationSpan>(); var htmlDoc = HtmlEditorDocumentFromTextBuffer(span.Snapshot.TextBuffer); if (htmlDoc == null) { return(spans); } if (_htmlDoc == null) { _htmlDoc = htmlDoc; _htmlDoc.HtmlEditorTree.UpdateCompleted += HtmlEditorTree_UpdateCompleted; } else { Debug.Assert(htmlDoc == _htmlDoc); } // If the tree is not up to date with respect to the current snapshot, then artifact ranges and the // associated parse results are also not up to date. We cannot force a refresh here because this // can potentially change the buffer, which is not legal for GetClassificationSpans to do, and will // break the editor. Queue the refresh for later, and asynchronously notify the editor that it needs // to re-classify once it's done. if (!_htmlDoc.HtmlEditorTree.IsReady) { Interlocked.Increment(ref _deferredClassifications); return(spans); } // The provided span may be in a projection snapshot, so we need to // map back to the source snapshot to find the correct // classification. If projSnapshot is null, we are already in the // correct snapshot. var projSnapshot = span.Snapshot as IProjectionSnapshot; var sourceSnapshot = span.Snapshot; var sourceStartIndex = span.Start.Position; if (projSnapshot != null) { var pt = projSnapshot.MapToSourceSnapshot(sourceStartIndex); sourceStartIndex = pt.Position; sourceSnapshot = pt.Snapshot; if (HtmlEditorDocument.TryFromTextBuffer(sourceSnapshot.TextBuffer) != _htmlDoc) { return(spans); } } var index = _htmlDoc.HtmlEditorTree.ArtifactCollection.GetItemContaining(sourceStartIndex); if (index < 0) { return(spans); } var artifact = _htmlDoc.HtmlEditorTree.ArtifactCollection[index] as TemplateArtifact; if (artifact == null) { return(spans); } int artifactStart = artifact.InnerRange.Start; var artifactText = _htmlDoc.HtmlEditorTree.ParseTree.Text.GetText(artifact.InnerRange.Start, artifact.InnerRange.Length); artifact.Parse(artifactText); var classifications = artifact.GetClassifications(); foreach (var classification in classifications) { var cls = GetClassification(classification.Classification); int clsStart = artifactStart + classification.Span.Start; int clsLen = Math.Min(sourceSnapshot.Length - clsStart, classification.Span.Length); var clsSpan = new SnapshotSpan(sourceSnapshot, clsStart, clsLen); if (projSnapshot != null) { foreach (var sp in projSnapshot.MapFromSourceSnapshot(clsSpan)) { spans.Add(new ClassificationSpan(new SnapshotSpan(span.Snapshot, sp), cls)); } } else { spans.Add(new ClassificationSpan(clsSpan, cls)); } } return(spans); }
public override IList <ClassificationSpan> GetClassificationSpans(SnapshotSpan span) { var spans = new List <ClassificationSpan>(); var htmlDoc = HtmlEditorDocument.TryFromTextBuffer(span.Snapshot.TextBuffer); if (htmlDoc == null) { return(spans); } if (_htmlDoc == null) { _htmlDoc = htmlDoc; _htmlDoc.HtmlEditorTree.UpdateCompleted += HtmlEditorTree_UpdateCompleted; } else { Debug.Assert(htmlDoc == _htmlDoc); } // If the tree is not up to date with respect to the current snapshot, then artifact ranges and the // associated parse results are also not up to date. We cannot force a refresh here because this // can potentially change the buffer, which is not legal for GetClassificationSpans to do, and will // break the editor. Queue the refresh for later, and asynchronously notify the editor that it needs // to re-classify once it's done. if (!_htmlDoc.HtmlEditorTree.IsReady) { Interlocked.Increment(ref _deferredClassifications); return(spans); } var projSnapshot = _htmlDoc.PrimaryView.TextSnapshot as IProjectionSnapshot; if (projSnapshot == null) { return(spans); } var primarySpans = projSnapshot.MapFromSourceSnapshot(span); foreach (var primarySpan in primarySpans) { var index = _htmlDoc.HtmlEditorTree.ArtifactCollection.GetItemContaining(primarySpan.Start); if (index < 0) { continue; } var artifact = _htmlDoc.HtmlEditorTree.ArtifactCollection[index] as TemplateArtifact; if (artifact == null) { continue; } var artifactStart = projSnapshot.MapToSourceSnapshot(artifact.InnerRange.Start); if (artifactStart.Snapshot != span.Snapshot) { continue; } var artifactText = _htmlDoc.HtmlEditorTree.ParseTree.Text.GetText(artifact.InnerRange); artifact.Parse(artifactText); var classifications = artifact.GetClassifications(); foreach (var classification in classifications) { var classificationSpan = ToClassificationSpan(classification, span.Snapshot, artifactStart.Position); spans.Add(classificationSpan); } } return(spans); }