本文整理汇总了C#中ICSharpCode.NRefactory.CSharp.Resolver.CSharpAstResolver类的典型用法代码示例。如果您正苦于以下问题:C# CSharpAstResolver类的具体用法?C# CSharpAstResolver怎么用?C# CSharpAstResolver使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CSharpAstResolver类属于ICSharpCode.NRefactory.CSharp.Resolver命名空间,在下文中一共展示了CSharpAstResolver类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ResolveCref
public override IEntity ResolveCref(string cref)
{
if (cref.Length > 2 && cref[1] == ':') {
// resolve ID string
return base.ResolveCref(cref);
}
var documentationReference = new CSharpParser().ParseDocumentationReference(cref);
var csharpContext = context as CSharpTypeResolveContext;
CSharpResolver resolver;
if (csharpContext != null) {
resolver = new CSharpResolver(csharpContext);
} else {
resolver = new CSharpResolver(context.Compilation);
}
var astResolver = new CSharpAstResolver(resolver, documentationReference);
var rr = astResolver.Resolve(documentationReference);
MemberResolveResult mrr = rr as MemberResolveResult;
if (mrr != null)
return mrr.Member;
TypeResolveResult trr = rr as TypeResolveResult;
if (trr != null)
return trr.Type.GetDefinition();
return null;
}
示例2: Rename
public RenameResponse Rename(RenameRequest req)
{
var project = _solution.ProjectContainingFile(req.FileName);
var syntaxTree = project.CreateParser().Parse(req.Buffer, req.FileName);
var sourceNode = syntaxTree.GetNodeAt(req.Line, req.Column);
if(sourceNode == null)
return new RenameResponse();
var originalName = sourceNode.GetText();
IEnumerable<AstNode> nodes = _findUsagesHandler.FindUsageNodes(req).ToArray();
var response = new RenameResponse();
var modfiedFiles = new List<ModifiedFileResponse>();
response.Changes = modfiedFiles;
foreach (IGrouping<string, AstNode> groupedNodes in nodes.GroupBy(n => n.GetRegion().FileName.FixPath()))
{
string fileName = groupedNodes.Key;
OmniSharpRefactoringContext context;
if (groupedNodes.Key != req.FileName)
{
var file = _solution.GetFile(fileName);
var bufferParser = new BufferParser(_solution);
var content = bufferParser.ParsedContent(file.Document.Text, file.FileName);
var resolver = new CSharpAstResolver(content.Compilation, content.SyntaxTree, content.UnresolvedFile);
context = new OmniSharpRefactoringContext(file.Document, resolver);
}
else
{
context = OmniSharpRefactoringContext.GetContext(_bufferParser, req);
}
string modifiedBuffer = null;
foreach (var node in groupedNodes.Where(n => n.GetText() == originalName))
{
using (var script = new OmniSharpScript(context))
{
script.Rename(node, req.RenameTo);
modifiedBuffer = script.CurrentDocument.Text;
}
}
if (modifiedBuffer != null)
{
var modifiedFile = new ModifiedFileResponse
{
FileName
= fileName,
Buffer = modifiedBuffer
};
modfiedFiles.Add(modifiedFile);
response.Changes = modfiedFiles;
_bufferParser.ParsedContent(modifiedBuffer, fileName);
_solution.GetFile(fileName).Update(modifiedBuffer);
}
}
return response;
}
示例3: RunTest
public static void RunTest(CSharpFile file)
{
CSharpAstResolver resolver = new CSharpAstResolver(file.Project.Compilation, file.CompilationUnit, file.ParsedFile);
var navigator = new ValidatingResolveAllNavigator(file.FileName);
resolver.ApplyNavigator(navigator, CancellationToken.None);
navigator.Validate(file.CompilationUnit);
}
示例4: GetAllValidTypesFromInvocation
static IEnumerable<IType> GetAllValidTypesFromInvocation(CSharpAstResolver resolver, InvocationExpression invoke, AstNode parameter)
{
int index = GetArgumentIndex(invoke.Arguments, parameter);
if (index < 0)
yield break;
var targetResult = resolver.Resolve(invoke.Target) as MethodGroupResolveResult;
if (targetResult != null) {
foreach (var method in targetResult.Methods) {
if (index < method.Parameters.Count) {
if (method.Parameters [index].IsParams) {
var arrayType = method.Parameters [index].Type as ArrayType;
if (arrayType != null)
yield return arrayType.ElementType;
}
yield return method.Parameters [index].Type;
}
}
foreach (var extMethods in targetResult.GetExtensionMethods ()) {
foreach (var extMethod in extMethods) {
if (index + 1 < extMethod.Parameters.Count) {
if (extMethod.Parameters [index + 1].IsParams) {
var arrayType = extMethod.Parameters [index + 1].Type as ArrayType;
if (arrayType != null)
yield return arrayType.ElementType;
}
yield return extMethod.Parameters [index + 1].Type;
}
}
}
}
}
示例5: ToolTipData
public ToolTipData (ICSharpCode.NRefactory.CSharp.SyntaxTree unit, ICSharpCode.NRefactory.Semantics.ResolveResult result, ICSharpCode.NRefactory.CSharp.AstNode node, CSharpAstResolver file)
{
this.Unit = unit;
this.Result = result;
this.Node = node;
this.Resolver = file;
}
示例6: GetSharedResolver
/// <summary>
/// Returns a full C# syntax tree resolver which is shared between semantic highlighting, source analysis and refactoring.
/// For code analysis tasks this should be used instead of generating an own resolver. Only exception is if a local resolving is done using a
/// resolve navigator.
/// Note: The shared resolver is fully resolved.
/// </summary>
public static Task<CSharpAstResolver> GetSharedResolver (this Document document)
{
var parsedDocument = document.ParsedDocument;
if (parsedDocument == null)
return null;
var unit = parsedDocument.GetAst<SyntaxTree> ();
var parsedFile = parsedDocument.ParsedFile as CSharpUnresolvedFile;
if (unit == null || parsedFile == null)
return null;
var resolverAnnotation = document.Annotation<ResolverAnnotation> ();
if (resolverAnnotation != null) {
if (resolverAnnotation.ParsedFile == parsedFile)
return resolverAnnotation.Task;
document.RemoveAnnotations<ResolverAnnotation> ();
}
var resolveTask = Task.Factory.StartNew (delegate {
var result = new CSharpAstResolver (document.Compilation, unit, parsedFile);
result.ApplyNavigator (new ConstantModeResolveVisitorNavigator (ResolveVisitorNavigationMode.Resolve, null));
return result;
});
document.AddAnnotation (new ResolverAnnotation {
Task = resolveTask,
ParsedFile = parsedFile
});
return resolveTask;
}
示例7: GetContext
public static OmniSharpRefactoringContext GetContext(BufferParser bufferParser, Request request)
{
var q = bufferParser.ParsedContent(request.Buffer, request.FileName);
var resolver = new CSharpAstResolver(q.Compilation, q.SyntaxTree, q.UnresolvedFile);
var doc = new StringBuilderDocument(request.Buffer);
var location = new TextLocation(request.Line, request.Column);
OmniSharpRefactoringContext refactoringContext;
if(request is CodeActionRequest)
{
var car = request as CodeActionRequest;
if(car.SelectionStartColumn.HasValue)
{
var startLocation
= new TextLocation(car.SelectionStartLine.Value, car.SelectionStartColumn.Value);
var endLocation
= new TextLocation(car.SelectionEndLine.Value, car.SelectionEndColumn.Value);
refactoringContext = new OmniSharpRefactoringContext(doc, resolver, location, startLocation, endLocation);
}
else
{
refactoringContext = new OmniSharpRefactoringContext(doc, resolver, location);
}
}
else
{
refactoringContext = new OmniSharpRefactoringContext(doc, resolver, location);
}
refactoringContext.Services.AddService (typeof(NamingConventionService), new DefaultNameService ());
return refactoringContext;
}
示例8: Resolve
public static ResolveResult Resolve(Lazy<ICompilation> compilation, CSharpUnresolvedFile unresolvedFile, SyntaxTree syntaxTree, TextLocation location, out AstNode node,
CancellationToken cancellationToken = default(CancellationToken))
{
node = syntaxTree.GetNodeAt(location);
if (node == null || node is ArrayInitializerExpression)
return null;
if (node.Parent is UsingAliasDeclaration && node.Role == UsingAliasDeclaration.AliasRole) {
var r = new CSharpAstResolver(compilation.Value, syntaxTree, unresolvedFile);
return r.Resolve(((UsingAliasDeclaration)node.Parent).Import, cancellationToken);
}
if (CSharpAstResolver.IsUnresolvableNode(node)) {
if (node is Identifier) {
node = node.Parent;
} else if (node.NodeType == NodeType.Token) {
if (node.Parent is IndexerExpression || node.Parent is ConstructorInitializer) {
// There's no other place where one could hover to see the indexer's tooltip,
// so we need to resolve it when hovering over the '[' or ']'.
// For constructor initializer, the same applies to the 'base'/'this' token.
node = node.Parent;
} else {
return null;
}
} else {
// don't resolve arbitrary nodes - we don't want to show tooltips for everything
return null;
}
} else {
// It's a resolvable node.
// However, we usually don't want to show the tooltip everywhere
// For example, hovering with the mouse over an empty line between two methods causes
// node==TypeDeclaration, but we don't want to show any tooltip.
if (!node.GetChildByRole(Roles.Identifier).IsNull) {
// We'll suppress the tooltip for resolvable nodes if there is an identifier that
// could be hovered over instead:
return null;
}
}
if (node == null)
return null;
if (node.Parent is ObjectCreateExpression && node.Role == Roles.Type) {
node = node.Parent;
}
InvocationExpression parentInvocation = null;
if ((node is IdentifierExpression || node is MemberReferenceExpression || node is PointerReferenceExpression) && node.Role != Roles.Argument) {
// we also need to resolve the invocation
parentInvocation = node.Parent as InvocationExpression;
}
// TODO: I think we should provide an overload so that an existing CSharpAstResolver can be reused
CSharpAstResolver resolver = new CSharpAstResolver(compilation.Value, syntaxTree, unresolvedFile);
ResolveResult rr = resolver.Resolve(node, cancellationToken);
if (rr is MethodGroupResolveResult && parentInvocation != null)
return resolver.Resolve(parentInvocation);
else
return rr;
}
示例9: RunTest
public static void RunTest(CSharpFile file)
{
CSharpAstResolver resolver = new CSharpAstResolver(file.Project.Compilation, file.SyntaxTree, file.UnresolvedTypeSystemForFile);
var navigator = new ValidatingResolveAllNavigator(file.FileName);
resolver.ApplyNavigator(navigator, CancellationToken.None);
navigator.Validate(resolver, file.SyntaxTree);
}
示例10: FindSemanticErrors
public SemanticErrorsResponse FindSemanticErrors(Request request)
{
var clientFilename = request.FileName.ApplyPathReplacementsForClient();
var project = _solution.ProjectContainingFile(request.FileName);
project.UpdateFile(request.FileName, request.Buffer);
var solutionSnapshot = new DefaultSolutionSnapshot(_solution.Projects.Select(i => i.ProjectContent));
SyntaxTree syntaxTree;
if(project.CompilerSettings!=null){
syntaxTree = new CSharpParser(project.CompilerSettings).Parse(request.Buffer, request.FileName);
}else{
syntaxTree = new CSharpParser().Parse(request.Buffer, request.FileName);
}
var resolver = new CSharpAstResolver(solutionSnapshot.GetCompilation(project.ProjectContent), syntaxTree);
var navigator = new SemanticErrorsNavigator();
resolver.ApplyNavigator(navigator);
var errors = navigator.GetErrors()
.Where(e => ShouldIncludeIssue(e.Message))
.Select(i => new Error
{
FileName = clientFilename,
Message = i.Message,
Line = i.StartLocation.Line,
Column = i.EndLocation.Column,
EndLine = i.EndLocation.Line,
EndColumn = i.EndLocation.Column
});
return new SemanticErrorsResponse
{
Errors = errors,
};
}
示例11: GetSharedResolver
/// <summary>
/// Returns a full C# syntax tree resolver which is shared between semantic highlighting, source analysis and refactoring.
/// For code analysis tasks this should be used instead of generating an own resolver. Only exception is if a local resolving is done using a
/// resolve navigator.
/// Note: The shared resolver is fully resolved.
/// </summary>
public static Task<CSharpAstResolver> GetSharedResolver (this Document document)
{
var parsedDocument = document.ParsedDocument;
if (parsedDocument == null || document.IsProjectContextInUpdate || document.Project != null && !(document.Project is DotNetProject))
return null;
var unit = parsedDocument.GetAst<SyntaxTree> ();
var parsedFile = parsedDocument.ParsedFile as CSharpUnresolvedFile;
if (unit == null || parsedFile == null)
return null;
var compilation = document.Compilation;
var resolverAnnotation = document.Annotation<ResolverAnnotation> ();
if (resolverAnnotation != null) {
if (resolverAnnotation.ParsedFile == parsedFile)
return resolverAnnotation.Task;
if (resolverAnnotation.SharedTokenSource != null)
resolverAnnotation.SharedTokenSource.Cancel ();
document.RemoveAnnotations<ResolverAnnotation> ();
}
var tokenSource = new CancellationTokenSource ();
var token = tokenSource.Token;
var resolveTask = Task.Factory.StartNew (delegate {
try {
using (var timer = ResolveCounter.BeginTiming ()) {
var result = new CSharpAstResolver (compilation, unit, parsedFile);
result.ApplyNavigator (new ConstantModeResolveVisitorNavigator (ResolveVisitorNavigationMode.Resolve, null), token);
return result;
}
} catch (OperationCanceledException) {
return null;
} catch (Exception e) {
LoggingService.LogError ("Error while creating the resolver.", e);
return null;
}
}, token);
var wrapper = resolveTask.ContinueWith (t => {
if (t.IsCanceled)
return null;
if (t.IsFaulted) {
var ex = t.Exception.Flatten ().InnerException;
if (!(ex is TaskCanceledException))
LoggingService.LogWarning ("Exception while getting shared AST resolver.", ex);
return null;
}
return t.Result;
}, TaskContinuationOptions.ExecuteSynchronously);
document.AddAnnotation (new ResolverAnnotation {
Task = wrapper,
ParsedFile = parsedFile,
SharedTokenSource = tokenSource
});
return wrapper;
}
示例12: JavaMethodVisitor
public JavaMethodVisitor(CSharpAstResolver resolver, StreamWriter writer)
{
this.m_Resolver = resolver;
this.m_Writer = writer;
this.m_BytecodeWriter = new JavaBytecodeWriter(writer);
this.m_Locals.Add("~dummy0", 0);
this.m_Locals.Add("~dummy1", 1);
}
示例13: TestRefactoringContext
public TestRefactoringContext (IDocument document, TextLocation location, CSharpAstResolver resolver) : base(resolver, CancellationToken.None)
{
this.doc = document;
this.location = location;
this.UseExplicitTypes = UseExplict;
UseExplict = false;
Services.AddService (typeof(NamingConventionService), new TestNameService ());
}
示例14: OmniSharpRefactoringContext
private OmniSharpRefactoringContext(IDocument document, CSharpAstResolver resolver, TextLocation location, TextLocation selectionStart, TextLocation selectionEnd)
: base(resolver, CancellationToken.None)
{
_document = document;
_location = location;
_selectionStart = selectionStart;
_selectionEnd = selectionEnd;
}
示例15: InitResolver
private void InitResolver(SyntaxTree syntaxTree)
{
if(this.lastFileName != syntaxTree.FileName) {
this.lastFileName = syntaxTree.FileName;
var unresolvedFile = syntaxTree.ToTypeSystem();
this.resolver = new CSharpAstResolver(compilation, syntaxTree, unresolvedFile);
}
}