Skip to content

Commit

Permalink
! Used ITextBuffer to access Roslyn
Browse files Browse the repository at this point in the history
  • Loading branch information
wmjordan committed Jun 28, 2019
1 parent 12d27a3 commit 5541cac
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 27 deletions.
10 changes: 5 additions & 5 deletions Codist/Helpers/CodeAnalysisHelper.Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ public static Document GetDocument(this Workspace workspace, SnapshotSpan span)
if (span.Snapshot == null) {
throw new InvalidOperationException("snapshot is null");
}
var sourceText = span.Snapshot.AsText();
if (sourceText == null) {
throw new InvalidOperationException("sourceText is null");
var textContainer = span.Snapshot.TextBuffer.AsTextContainer();
if (textContainer == null) {
throw new InvalidOperationException("textContainer is null");
}
var docId = workspace.GetDocumentIdInCurrentContext(sourceText.Container);
var docId = workspace.GetDocumentIdInCurrentContext(textContainer);
if (docId == null) {
throw new InvalidOperationException("docId is null");
}
return solution.ContainsDocument(docId)
? solution.GetDocument(docId)
: solution.WithDocumentText(docId, sourceText, PreservationMode.PreserveIdentity).GetDocument(docId);
: solution.WithDocumentText(docId, textContainer.CurrentText, PreservationMode.PreserveIdentity).GetDocument(docId);
}

/// <summary>Gets all <see cref="Document"/>s from a given <see cref="Project"/> and referencing/referenced projects.</summary>
Expand Down
16 changes: 8 additions & 8 deletions Codist/Helpers/SemanticContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,12 @@ public async Task<ISymbol> GetSymbolAsync(CancellationToken cancellationToken) {

public async Task<bool> UpdateAsync(CancellationToken cancellationToken) {
try {
var text = View.TextSnapshot.AsText();
var textContainer = View.TextBuffer.AsTextContainer();
Document doc = null;
if (Workspace.TryGetWorkspace(text.Container, out var workspace)) {
var id = workspace.GetDocumentIdInCurrentContext(text.Container);
if (Workspace.TryGetWorkspace(textContainer, out var workspace)) {
var id = workspace.GetDocumentIdInCurrentContext(textContainer);
if (id != null && workspace.CurrentSolution.ContainsDocument(id)) {
doc = workspace.CurrentSolution.WithDocumentText(id, text, PreservationMode.PreserveIdentity).GetDocument(id);
doc = workspace.CurrentSolution.WithDocumentText(id, textContainer.CurrentText, PreservationMode.PreserveIdentity).GetDocument(id);
}
}
if (doc != Document) {
Expand All @@ -256,13 +256,13 @@ public async Task<bool> UpdateAsync(CancellationToken cancellationToken) {
public async Task<bool> UpdateAsync(int position, CancellationToken cancellationToken) {
bool versionChanged;
try {
var text = View.TextSnapshot.AsText();
var textContainer = View.TextBuffer.AsTextContainer();
Document = null;
if (Workspace.TryGetWorkspace(text.Container, out var workspace)) {
if (Workspace.TryGetWorkspace(textContainer, out var workspace)) {
Workspace = workspace;
var id = workspace.GetDocumentIdInCurrentContext(text.Container);
var id = workspace.GetDocumentIdInCurrentContext(textContainer);
if (id != null && workspace.CurrentSolution.ContainsDocument(id)) {
Document = workspace.CurrentSolution.WithDocumentText(id, text, PreservationMode.PreserveIdentity).GetDocument(id);
Document = workspace.CurrentSolution.WithDocumentText(id, textContainer.CurrentText, PreservationMode.PreserveIdentity).GetDocument(id);
}
}
var ver = await Document.GetTextVersionAsync(cancellationToken).ConfigureAwait(false);
Expand Down
26 changes: 12 additions & 14 deletions Codist/QuickInfo/CSharpQuickInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ sealed class CSharpQuickInfo : IQuickInfoSource
static readonly SymbolFormatter _SymbolFormatter = SymbolFormatter.Instance;

readonly bool _IsVsProject;
readonly ITextBuffer _TextBuffer;
bool _IsDisposed;
SemanticModel _SemanticModel;
readonly ITextBuffer _TextBuffer;

public CSharpQuickInfo(ITextBuffer subjectBuffer) {
ThreadHelper.ThrowIfNotOnUIThread();
Expand Down Expand Up @@ -54,7 +54,7 @@ public void AugmentQuickInfoSession(IQuickInfoSession session, IList<object> qiC
}

SemanticModel semanticModel;
var container = currentSnapshot.AsText().Container;
var container = _TextBuffer.AsTextContainer();
DocumentId docId;
if (Workspace.TryGetWorkspace(container, out var workspace) == false
|| (docId = workspace.GetDocumentIdInCurrentContext(container)) == null
Expand Down Expand Up @@ -86,9 +86,7 @@ public void AugmentQuickInfoSession(IQuickInfoSession session, IList<object> qiC
if (Config.Instance.QuickInfoOptions.MatchFlags(QuickInfoOptions.Parameter)) {
break;
}
else {
goto EXIT;
}
goto EXIT;
case SyntaxKind.AsKeyword:
var asType = (unitCompilation.FindNode(token.Span) as BinaryExpressionSyntax)?.GetLastIdentifier();
if (asType != null) {
Expand Down Expand Up @@ -659,6 +657,15 @@ static void ShowNamespaceInfo(IList<object> qiContent, INamespaceSymbol nsSymbol
}

void ShowTypeInfo(IList<object> qiContent, SyntaxNode node, INamedTypeSymbol typeSymbol) {
if (Config.Instance.QuickInfoOptions.MatchFlags(QuickInfoOptions.MethodOverload)) {
node = node.GetObjectCreationNode();
if (node != null) {
var method = _SemanticModel.GetSymbolOrFirstCandidate(node) as IMethodSymbol;
if (method != null) {
ShowOverloadsInfo(qiContent, node, method);
}
}
}
if (Config.Instance.QuickInfoOptions.MatchFlags(QuickInfoOptions.BaseType)) {
if (typeSymbol.TypeKind == TypeKind.Enum) {
ShowEnumInfo(qiContent, typeSymbol, true);
Expand All @@ -675,15 +682,6 @@ void ShowTypeInfo(IList<object> qiContent, SyntaxNode node, INamedTypeSymbol typ
&& (typeSymbol.DeclaredAccessibility != Accessibility.Public || typeSymbol.IsAbstract || typeSymbol.IsStatic || typeSymbol.IsSealed)) {
ShowDeclarationModifier(qiContent, typeSymbol);
}
if (Config.Instance.QuickInfoOptions.MatchFlags(QuickInfoOptions.MethodOverload)) {
node = node.GetObjectCreationNode();
if (node != null) {
var method = _SemanticModel.GetSymbolOrFirstCandidate(node) as IMethodSymbol;
if (method != null) {
ShowOverloadsInfo(qiContent, node, method);
}
}
}
if (Config.Instance.QuickInfoOptions.MatchFlags(QuickInfoOptions.InterfaceMembers)
&& typeSymbol.TypeKind == TypeKind.Interface) {
ShowInterfaceMembers(qiContent, typeSymbol);
Expand Down

0 comments on commit 5541cac

Please sign in to comment.