Syntax.NormalizeWhitespace() Missing? - roslyn

There is some code which use SyntaxExtensions.NormalizeWhitespace().
I cannot find the method inside recent Microsoft.CodeAnalysis.CSharp releases.
Is there a quick way to format syntax using roslyn?

The method is still there. It's surfaced as an extension method on SyntaxNode, SyntaxToken, and SyntaxList.
The easiest way to use this is to call the method on an existing SyntaxNode. For example, in a code fix, I might do something like this:
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var newRoot = root.NormalizeWhitespace();
Document updatedDocument = document.WithSyntaxRoot(newRoot);

Related

In Clang, can I access the SourceManager when writing a custom ASTMatcher?

I'm trying to upgrade my AutoFFI project by making it more elegant and use Clang's ASTMatchers more extensively. I'd like to create a matcher that filters on the file path that was specified. Is it possible to do such a thing, or do I need to add custom logic outside of the matcher for this to work? As far as I can see, there's no way to get the SourceManager and use it to create a FullSourceLoc, but maybe I'm missing something.
Some relevant links:
https://clang.llvm.org/doxygen/classclang_1_1FullSourceLoc.html
https://github.com/llvm-mirror/clang/blob/f3b7928366f63b51ffc97e74f8afcff497c57e8d/include/clang/ASTMatchers/ASTMatchersMacros.h#L28
If someone could tell me whether this is a limitation to Clang's ASTMatcher API or not I'd be very grateful!
Never mind, I've found the answer by looking at the source of isExpansionInMainFile:
AST_POLYMORPHIC_MATCHER(isExpansionInMainFile,
AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc)) {
auto &SourceManager = Finder->getASTContext().getSourceManager();
return SourceManager.isInMainFile(
SourceManager.getExpansionLoc(Node.getBeginLoc()));
}
Turns out I missed the getASTContext in MatchFinder, which holds on to the source manager.

VSIX how to get current snapshot document name?

I have been trying to to create an extension that highlights specific line numbers for me in Visual Studio in the margins.
I manged to get my marking in the margins using predefined line number but for it to work properly I need to know what the current document FullName is (Path and filename)
After much googling I figured out how to do it with the sample code (which is not ideal)
DTE2 dte = (DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.15.0");
var activeDocument = dte.ActiveDocument;
var docName = activeDocument.Name;
var docFullName = activeDocument.FullName;
Now I know the problems here
is that is for specific version bases on the text
there is no way to select which instance (when running more than one VS)
It seems to be very slow
I have a feeling I should be doing this with MEF Attributes but the MS docs examples are so simple that they do not work for me. I scanned a few SO questions too and I just cannot get them to work. They mostly talk about Services.. which I do not have and have no idea how to get.
The rest of my code uses SnapshotSpans as in the example Extension of Todo_Classification examples which is great if you do NOT need to know the file name.
I have never done any extensions development. Please can somebody help me do this correctly.
You can use following code to get a file from a snapshot without any dependencies.
public string GetDocumentPath(Microsoft.VisualStudio.Text.ITextSnapshot ts)
{
Microsoft.VisualStudio.Text.ITextDocument textDoc;
bool rc = ts.TextBuffer.Properties.TryGetProperty(
typeof(Microsoft.VisualStudio.Text.ITextDocument), out textDoc);
if (rc && textDoc != null)
return textDoc.FilePath;
return null;
}
If you don't mind adding Microsoft.CodeAnalysis.EditorFeatures.Text to your project it will provide you with an extension method Document GetOpenDocumentInCurrentContextWithChanges() on the Microsoft.VisualStudio.Text.Snapshot class. (Plus many other Rosyln based helpers)
using Microsoft.CodeAnalysis.Text;
Document doc = span.Snapshot.GetOpenDocumentInCurrentContextWithChanges();

How to use an Ember.getOwner(this).lookup('application:main') result

As the title say, I have some problem understanding what does this call return.
This is how I am using it:
fetchEngines()
{
let object = Ember.getOwner(this).lookup('application:main').engines;
console.log(object);
}
And it return me something like that:
At this point, this is what I want, the list of all my ember-engines.
But I don't know how to use it. By that I mean, how do I fetch the name of each engine, what is object at this point, I can't find anything about it.
I have tried the forEach() method, but it returns me : object.forEach is not a function. I have also tried the Object.keys method, but it returned me undefined, maybe somebody can indicate me a doc or something, I don't understand at all what is it.
Good day to you and thank you for reading.
I will answer this. This is very simple, and I made a mistake. The Object.keys method work, I didn't know how to write it well.
This is the corrected version:
fetchEngines()
{
let object = Ember.getOwner(this).lookup('application:main').engines;
// This will properly show every key in your object
console.log(Object.keys(object));
// And if you want to enumerate it
let filledArray = [];
for (let key in object) {
if (object.hasOwnProperty(key))
filledArray.push(key);
}
// The object filledArray is now a perfectly manipulable object
}

Meteor - How to use Blaze.renderWithData in a bootbox and have the result remain reactive?

I am using the following:
let box = bootbox.dialog({title:'',message:''});
box.find('.bootbox-body').remove();
Blaze.renderWithData(template,doc,box.find(".modal-body")[0]);
It renders correctly, but is not reactive.
I suspect I have a problem passing in the doc directly, and have the _id of the doc available.
What should I be passing to renderWithData in order for the result to be reactive?
If in the code before the bootbox you have doc = MyCollection.findOne(...) then doc will be reactive. Otherwise you can pass in the _id and perform the .find() in your template helper (the one you are passing into Blaze.render().
I found my solution.
Instead of
let doc = MyCollection.findOne({_id});
Blaze.renderWithData(template,doc,box.find(".modal-body")[0]);
Or
Blaze.renderWithData(template,MyCollection.findOne({_id}),box.find(".modal-body")[0]);
I switched to
Blaze.renderWithData(template,function(){
MyCollection.findOne({_id})
},box.find(".modal-body")[0]);
This now makes the dialog reactive.

Extension method information from Roslyn

I am trying to using Roslyn to extract various method call information from a give source file. To elaborate - I want to find all method invocations that happen inside the input file.
One problem I am hitting is to do with extension methods. Consider any Linq method like Aggregate, Sum etc. How can I figure out from an InvocationExpressionSyntax that the method being invoked is an extension method and not a simple member method of the class.
The source file I input can be expected to compile - meaning that GetDiagnostics() will not have any errors.
---- Some code to get to the property mentioned by #Kevin in his answer ----
var methodInfo = model.GetSymbolInfo(invocation);
if (methodInfo.Symbol != null)
{
var mSymbol = (IMethodSymbol)methodInfo.Symbol;
if (mSymbol.ReducedFrom != null)
{
// this is an extension method !
}
}
You can find the actual static extension method for an instance invocation of an extension method using the IMethodSymbol.ReducedFrom property.