提交 a5c02e2c 编写于 作者: K Kevin Halverson

Merge pull request #5434 from KevinH-MS/master

Add null checks when calling IWpfTextView.TextViewModel...
......@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
......@@ -365,6 +366,13 @@ public virtual bool TryHandleReturn()
public virtual bool TryInsertExpansion(int startPositionInSubjectBuffer, int endPositionInSubjectBuffer)
{
var textViewModel = TextView.TextViewModel;
if (textViewModel == null)
{
Debug.Assert(TextView.IsClosed);
return false;
}
int startLine = 0;
int startIndex = 0;
int endLine = 0;
......@@ -377,13 +385,13 @@ public virtual bool TryInsertExpansion(int startPositionInSubjectBuffer, int end
SnapshotSpan dataBufferSpan;
if (!TryGetSpanOnHigherBuffer(
SubjectBuffer.CurrentSnapshot.GetSpan(startPositionInSubjectBuffer, endPositionInSubjectBuffer - startPositionInSubjectBuffer),
TextView.TextViewModel.DataBuffer,
textViewModel.DataBuffer,
out dataBufferSpan))
{
return false;
}
var buffer = EditorAdaptersFactoryService.GetBufferAdapter(TextView.TextViewModel.DataBuffer);
var buffer = EditorAdaptersFactoryService.GetBufferAdapter(textViewModel.DataBuffer);
var expansion = buffer as IVsExpansion;
if (buffer == null || expansion == null)
{
......@@ -446,6 +454,13 @@ public int OnBeforeInsertion(IVsExpansionSession pSession)
public int OnItemChosen(string pszTitle, string pszPath)
{
var textViewModel = TextView.TextViewModel;
if (textViewModel == null)
{
Debug.Assert(TextView.IsClosed);
return VSConstants.E_FAIL;
}
var hr = VSConstants.S_OK;
try
......@@ -456,7 +471,7 @@ public int OnItemChosen(string pszTitle, string pszPath)
textSpan.iEndLine = textSpan.iStartLine;
textSpan.iEndIndex = textSpan.iStartIndex;
IVsExpansion expansion = EditorAdaptersFactoryService.GetBufferAdapter(TextView.TextViewModel.DataBuffer) as IVsExpansion;
IVsExpansion expansion = EditorAdaptersFactoryService.GetBufferAdapter(textViewModel.DataBuffer) as IVsExpansion;
earlyEndExpansionHappened = false;
hr = expansion.InsertNamedExpansion(pszTitle, pszPath, textSpan, this, LanguageServiceGuid, fShowDisambiguationUI: 0, pSession: out ExpansionSession);
......
......@@ -53,12 +53,20 @@ public override int GetDataTipText(TextSpan[] pSpan, out string pbstrText)
return VSConstants.E_INVALIDARG;
}
var textViewModel = WpfTextView.TextViewModel;
if (textViewModel == null)
{
Debug.Assert(WpfTextView.IsClosed);
pbstrText = null;
return VSConstants.E_FAIL;
}
// We need to map the TextSpan from the DataBuffer to our subject buffer. We'll
// only consider spans whose length matches our input span (which we expect to
// always be zero). This is to address the case where the position is on a seam
// and maps to multiple source spans.
// If there is not exactly one matching span, just return.
var span = WpfTextView.TextViewModel.DataBuffer.CurrentSnapshot.GetSpan(pSpan[0]);
var span = textViewModel.DataBuffer.CurrentSnapshot.GetSpan(pSpan[0]);
var spanLength = span.Length;
Debug.Assert(spanLength == 0, $"Expected zero length span (got '{spanLength}'.");
var subjectSpan = WpfTextView.BufferGraph.MapDownToBuffer(span, SpanTrackingMode.EdgeInclusive, _subjectBuffer)
......@@ -83,7 +91,7 @@ public override int GetDataTipText(TextSpan[] pSpan, out string pbstrText)
// take the span that intersects with the input span, since that's probably
// the one we care about.
// If there are no such spans, just return.
var surfaceSpan = WpfTextView.BufferGraph.MapUpToBuffer(subjectSpan, SpanTrackingMode.EdgeInclusive, WpfTextView.TextViewModel.DataBuffer)
var surfaceSpan = WpfTextView.BufferGraph.MapUpToBuffer(subjectSpan, SpanTrackingMode.EdgeInclusive, textViewModel.DataBuffer)
.SingleOrDefault(x => x.IntersectsWith(span));
if (surfaceSpan == default(SnapshotSpan))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册