本文整理汇总了C#中ICSharpCode.NRefactory.CSharp.Resolver.CSharpAstResolver.GetResolverStateBefore方法的典型用法代码示例。如果您正苦于以下问题:C# CSharpAstResolver.GetResolverStateBefore方法的具体用法?C# CSharpAstResolver.GetResolverStateBefore怎么用?C# CSharpAstResolver.GetResolverStateBefore使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICSharpCode.NRefactory.CSharp.Resolver.CSharpAstResolver
的用法示例。
在下文中一共展示了CSharpAstResolver.GetResolverStateBefore方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ResolveContext
public ICodeContext ResolveContext(ParseInformation parseInfo, TextLocation location, ICompilation compilation, CancellationToken cancellationToken)
{
var decompiledParseInfo = parseInfo as ILSpyFullParseInformation;
if (decompiledParseInfo == null)
throw new ArgumentException("ParseInfo does not have SyntaxTree");
var syntaxTree = decompiledParseInfo.SyntaxTree;
var node = syntaxTree.GetNodeAt(location);
if (node == null)
return null; // null result is allowed; the parser service will substitute a dummy context
var resolver = new CSharpAstResolver(compilation, syntaxTree, null);
return resolver.GetResolverStateBefore(node);
}
示例2: MagicKeyCompletion
//.........这里部分代码省略.........
contextList.AddCustom("field");
contextList.AddCustom("property");
contextList.AddCustom("method");
contextList.AddCustom("event");
}
contextList.AddCustom("return");
}
if (n is MemberType) {
resolveResult = ResolveExpression(
((MemberType)n).Target,
identifierStart.Unit
);
return CreateTypeAndNamespaceCompletionData(
location,
resolveResult.Item1,
((MemberType)n).Target,
resolveResult.Item2
);
}
if (n != null/* && !(identifierStart.Item2 is TypeDeclaration)*/) {
csResolver = new CSharpResolver(ctx);
var nodes = new List<AstNode>();
nodes.Add(n);
if (n.Parent is ICSharpCode.NRefactory.CSharp.Attribute) {
nodes.Add(n.Parent);
}
var astResolver = new CSharpAstResolver(
csResolver,
identifierStart.Unit,
CSharpParsedFile
);
astResolver.ApplyNavigator(new NodeListResolveVisitorNavigator(nodes));
try {
csResolver = astResolver.GetResolverStateBefore(n);
} catch (Exception) {
csResolver = GetState();
}
// add attribute properties.
if (n.Parent is ICSharpCode.NRefactory.CSharp.Attribute) {
var resolved = astResolver.Resolve(n.Parent);
if (resolved != null && resolved.Type != null) {
foreach (var property in resolved.Type.GetProperties (p => p.Accessibility == Accessibility.Public)) {
contextList.AddMember(property);
}
foreach (var field in resolved.Type.GetFields (p => p.Accessibility == Accessibility.Public)) {
contextList.AddMember(field);
}
}
}
} else {
csResolver = GetState();
}
// identifier has already started with the first letter
offset--;
AddContextCompletion(
contextList,
csResolver,
identifierStart.Node,
identifierStart.Unit
);
return contextList.Result;
// if (stub.Parent is BlockStatement)
// result = FindExpression (dom, completionContext, -1);
// if (result == null)
// return null;
示例3: GetValidTypes
//.........这里部分代码省略.........
if (expr.Parent is InvocationExpression) {
var parent = expr.Parent;
if (parent is InvocationExpression) {
var invoke = (InvocationExpression)parent;
return GetAllValidTypesFromInvocation(resolver, invoke, expr);
}
}
if (expr.Parent is VariableInitializer) {
var initializer = (VariableInitializer)expr.Parent;
var field = initializer.GetParent<FieldDeclaration>();
if (field != null) {
var rr = resolver.Resolve(field.ReturnType);
if (!rr.IsError)
return new [] { rr.Type };
}
var varStmt = initializer.GetParent<VariableDeclarationStatement>();
if (varStmt != null) {
var rr = resolver.Resolve(varStmt.Type);
if (!rr.IsError)
return new [] { rr.Type };
}
return new [] { resolver.Resolve(initializer).Type };
}
if (expr.Parent is CastExpression) {
var cast = (CastExpression)expr.Parent;
return new [] { resolver.Resolve(cast.Type).Type };
}
if (expr.Parent is AsExpression) {
var cast = (AsExpression)expr.Parent;
return new [] { resolver.Resolve(cast.Type).Type };
}
if (expr.Parent is AssignmentExpression) {
var assign = (AssignmentExpression)expr.Parent;
var other = assign.Left == expr ? assign.Right : assign.Left;
return new [] { resolver.Resolve(other).Type };
}
if (expr.Parent is BinaryOperatorExpression) {
var assign = (BinaryOperatorExpression)expr.Parent;
var other = assign.Left == expr ? assign.Right : assign.Left;
return new [] { resolver.Resolve(other).Type };
}
if (expr.Parent is ReturnStatement) {
var parent = expr.Ancestors.FirstOrDefault(n => n is EntityDeclaration || n is AnonymousMethodExpression|| n is LambdaExpression);
if (parent != null) {
var rr = resolver.Resolve(parent);
if (!rr.IsError)
return new [] { rr.Type };
}
var e = parent as EntityDeclaration;
if (e != null) {
var rt = resolver.Resolve(e.ReturnType);
if (!rt.IsError)
return new [] { rt.Type };
}
}
if (expr.Parent is YieldReturnStatement) {
var state = resolver.GetResolverStateBefore(expr);
if (state != null && (state.CurrentMember.ReturnType is ParameterizedType)) {
var pt = (ParameterizedType)state.CurrentMember.ReturnType;
if (pt.FullName == "System.Collections.Generic.IEnumerable") {
return new [] { pt.TypeArguments.First() };
}
}
}
if (expr.Parent is UnaryOperatorExpression) {
var uop = (UnaryOperatorExpression)expr.Parent;
switch (uop.Operator) {
case UnaryOperatorType.Not:
return new [] { resolver.Compilation.FindType(KnownTypeCode.Boolean) };
case UnaryOperatorType.Minus:
case UnaryOperatorType.Plus:
case UnaryOperatorType.Increment:
case UnaryOperatorType.Decrement:
case UnaryOperatorType.PostIncrement:
case UnaryOperatorType.PostDecrement:
return new [] { resolver.Compilation.FindType(KnownTypeCode.Int32) };
}
}
if (expr.Parent is ConstructorInitializer)
return GuessFromConstructorInitializer(resolver, expr);
if (expr.Parent is NamedExpression) {
var rr = resolver.Resolve(expr.Parent);
if (!rr.IsError) {
return new [] { rr.Type };
}
}
return Enumerable.Empty<IType>();
}
示例4: CodeGenerationOptions
public CodeGenerationOptions ()
{
currentState = new Lazy<CSharpResolver> (() => {
var parsedDocument = Document.ParsedDocument;
if (parsedDocument == null)
return null;
var unit = parsedDocument.GetAst<SyntaxTree> ().Clone ();
var file = parsedDocument.ParsedFile as CSharpUnresolvedFile;
var resolvedNode = unit.GetNodeAt<BlockStatement> (Document.Editor.Caret.Location);
if (resolvedNode == null)
return null;
var expr = new IdentifierExpression ("foo");
resolvedNode.Add (expr);
var ctx = file.GetTypeResolveContext (Document.Compilation, Document.Editor.Caret.Location);
var resolver = new CSharpResolver (ctx);
var astResolver = new CSharpAstResolver (resolver, unit, file);
astResolver.ApplyNavigator (new NodeListResolveVisitorNavigator (expr), CancellationToken.None);
astResolver.Resolve (expr);
return astResolver.GetResolverStateBefore (expr);
});
}
示例5: GetLanguageItem
public ResolveResult GetLanguageItem (MonoDevelop.Ide.Gui.Document doc, int offset, string expression)
{
if (offset < 0) {
return null;
}
var parsedDocument = doc.ParsedDocument;
if (parsedDocument == null)
return null;
var data = doc.Editor;
var loc = data.OffsetToLocation (offset);
var unit = parsedDocument.GetAst<CompilationUnit> ();
var parsedFile = parsedDocument.ParsedFile as CSharpParsedFile;
if (unit == null || parsedFile == null) {
return null;
}
var node = unit.GetNodeAt (loc);
if (node == null) {
return null;
}
var resolver = new CSharpAstResolver (doc.Compilation, unit, parsedFile);
resolver.ApplyNavigator (new NodeListResolveVisitorNavigator (node), CancellationToken.None);
var state = resolver.GetResolverStateBefore (node, CancellationToken.None);
return state.LookupSimpleNameOrTypeName (expression, new List<IType> (), NameLookupMode.Expression);
}
示例6: GetValidTypes
internal static IEnumerable<IType> GetValidTypes(CSharpAstResolver resolver, Expression expr)
{
if (expr.Parent is DirectionExpression) {
var parent = expr.Parent.Parent;
if (parent is InvocationExpression) {
var invoke = (InvocationExpression)parent;
return GetAllValidTypesFromInvokation(resolver, invoke, expr.Parent);
}
}
if (expr.Parent is ArrayInitializerExpression) {
if (expr is NamedExpression)
return new [] { resolver.Resolve(((NamedExpression)expr).Expression).Type };
var aex = expr.Parent as ArrayInitializerExpression;
if (aex.IsSingleElement)
aex = aex.Parent as ArrayInitializerExpression;
var type = GetElementType(resolver, resolver.Resolve(aex.Parent).Type);
if (type.Kind != TypeKind.Unknown)
return new [] { type };
}
if (expr.Parent is ObjectCreateExpression) {
var invoke = (ObjectCreateExpression)expr.Parent;
return GetAllValidTypesFromObjectCreation(resolver, invoke, expr);
}
if (expr.Parent is ArrayCreateExpression) {
var ace = (ArrayCreateExpression)expr.Parent;
if (!ace.Type.IsNull) {
return new [] { resolver.Resolve(ace.Type).Type };
}
}
if (expr.Parent is InvocationExpression) {
var parent = expr.Parent;
if (parent is InvocationExpression) {
var invoke = (InvocationExpression)parent;
return GetAllValidTypesFromInvokation(resolver, invoke, expr);
}
}
if (expr.Parent is VariableInitializer) {
var initializer = (VariableInitializer)expr.Parent;
var field = initializer.GetParent<FieldDeclaration>();
if (field != null)
return new [] { resolver.Resolve(field.ReturnType).Type };
return new [] { resolver.Resolve(initializer).Type };
}
if (expr.Parent is CastExpression) {
var cast = (CastExpression)expr.Parent;
return new [] { resolver.Resolve(cast.Type).Type };
}
if (expr.Parent is AsExpression) {
var cast = (AsExpression)expr.Parent;
return new [] { resolver.Resolve(cast.Type).Type };
}
if (expr.Parent is AssignmentExpression) {
var assign = (AssignmentExpression)expr.Parent;
var other = assign.Left == expr ? assign.Right : assign.Left;
return new [] { resolver.Resolve(other).Type };
}
if (expr.Parent is BinaryOperatorExpression) {
var assign = (BinaryOperatorExpression)expr.Parent;
var other = assign.Left == expr ? assign.Right : assign.Left;
return new [] { resolver.Resolve(other).Type };
}
if (expr.Parent is ReturnStatement) {
var state = resolver.GetResolverStateBefore(expr.Parent);
if (state != null && state.CurrentMember != null)
return new [] { state.CurrentMember.ReturnType };
}
if (expr.Parent is YieldReturnStatement) {
var state = resolver.GetResolverStateBefore(expr);
if (state != null && (state.CurrentMember.ReturnType is ParameterizedType)) {
var pt = (ParameterizedType)state.CurrentMember.ReturnType;
if (pt.FullName == "System.Collections.Generic.IEnumerable") {
return new [] { pt.TypeArguments.First() };
}
}
}
if (expr.Parent is UnaryOperatorExpression) {
var uop = (UnaryOperatorExpression)expr.Parent;
switch (uop.Operator) {
case UnaryOperatorType.Not:
return new [] { resolver.Compilation.FindType(KnownTypeCode.Boolean) };
case UnaryOperatorType.Minus:
case UnaryOperatorType.Plus:
case UnaryOperatorType.Increment:
case UnaryOperatorType.Decrement:
case UnaryOperatorType.PostIncrement:
case UnaryOperatorType.PostDecrement:
return new [] { resolver.Compilation.FindType(KnownTypeCode.Int32) };
//.........这里部分代码省略.........
示例7: Main
public static void Main(string[] args)
{
if (args.Length == 0) {
Console.WriteLine("Please specify the path to a .sln file on the command line");
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
return;
}
Solution solution = new Solution(args[0]);
foreach (var file in solution.AllFiles) {
var astResolver = new CSharpAstResolver(file.Project.Compilation, file.SyntaxTree, file.UnresolvedTypeSystemForFile);
foreach (var invocation in file.SyntaxTree.Descendants.OfType<InvocationExpression>()) {
// Retrieve semantics for the invocation
var rr = astResolver.Resolve(invocation) as InvocationResolveResult;
if (rr == null) {
// Not an invocation resolve result - e.g. could be a UnknownMemberResolveResult instead
continue;
}
if (rr.Member.FullName != "System.String.IndexOf") {
// Invocation isn't a string.IndexOf call
continue;
}
if (rr.Member.Parameters.First().Type.FullName != "System.String") {
// Ignore the overload that accepts a char, as that doesn't take a StringComparison.
// (looking for a char always performs the expected ordinal comparison)
continue;
}
if (rr.Member.Parameters.Last().Type.FullName == "System.StringComparison") {
// Already using the overload that specifies a StringComparison
continue;
}
Console.WriteLine(invocation.GetRegion() + ": " + invocation.GetText());
file.IndexOfInvocations.Add(invocation);
}
}
Console.WriteLine("Found {0} places to refactor in {1} files.",
solution.AllFiles.Sum(f => f.IndexOfInvocations.Count),
solution.AllFiles.Count(f => f.IndexOfInvocations.Count > 0));
Console.Write("Apply refactorings? ");
string answer = Console.ReadLine();
if ("yes".Equals(answer, StringComparison.OrdinalIgnoreCase) || "y".Equals(answer, StringComparison.OrdinalIgnoreCase)) {
foreach (var file in solution.AllFiles) {
if (file.IndexOfInvocations.Count == 0)
continue;
// DocumentScript expects the the AST to stay unmodified (so that it fits
// to the document state at the time of the DocumentScript constructor call),
// so we call Freeze() to prevent accidental modifications (e.g. forgetting a Clone() call).
file.SyntaxTree.Freeze();
// AST resolver used to find context for System.StringComparison generation
var compilation = file.Project.Compilation;
var astResolver = new CSharpAstResolver(compilation, file.SyntaxTree, file.UnresolvedTypeSystemForFile);
// Create a document containing the file content:
var document = new StringBuilderDocument(file.OriginalText);
var formattingOptions = FormattingOptionsFactory.CreateAllman();
var options = new TextEditorOptions();
using (var script = new DocumentScript(document, formattingOptions, options)) {
foreach (InvocationExpression expr in file.IndexOfInvocations) {
// Generate a reference to System.StringComparison in this context:
var astBuilder = new TypeSystemAstBuilder(astResolver.GetResolverStateBefore(expr));
IType stringComparison = compilation.FindType(typeof(StringComparison));
AstType stringComparisonAst = astBuilder.ConvertType(stringComparison);
// Alternative 1: clone a portion of the AST and modify it
var copy = (InvocationExpression)expr.Clone();
copy.Arguments.Add(stringComparisonAst.Member("Ordinal"));
script.Replace(expr, copy);
// // Alternative 2: perform direct text insertion
// int offset = script.GetCurrentOffset(expr.RParToken.StartLocation);
// script.InsertText(offset, ", " + stringComparisonAst.GetText() + ".Ordinal");
}
}
File.WriteAllText(Path.ChangeExtension(file.FileName, ".output.cs"), document.Text);
}
}
}
示例8: ResolveContext
public ICodeContext ResolveContext(ParseInformation parseInfo, TextLocation location, ICompilation compilation, CancellationToken cancellationToken)
{
var csParseInfo = parseInfo as CSharpFullParseInformation;
if (csParseInfo == null)
throw new ArgumentException("Parse info does not have SyntaxTree");
CSharpUnresolvedFile unresolvedFile = csParseInfo.UnresolvedFile;
var projectContents = compilation.Assemblies.Select(asm => asm.UnresolvedAssembly).OfType<IProjectContent>().ToList();
if (projectContents.All(pc => pc.GetFile(unresolvedFile.FileName) != unresolvedFile))
unresolvedFile = null;
var syntaxTree = csParseInfo.SyntaxTree;
var node = syntaxTree.GetNodeAt(location);
if (node == null)
return null; // null result is allowed; the parser service will substitute a dummy context
var resolver = new CSharpAstResolver(compilation, syntaxTree, unresolvedFile);
return resolver.GetResolverStateBefore(node);
}
示例9: ResolveExpression
protected Tuple<ResolveResult, CSharpResolver> ResolveExpression(AstNode expr, CompilationUnit unit)
{
if (expr == null) {
return null;
}
AstNode resolveNode;
if (expr is Expression || expr is AstType) {
resolveNode = expr;
} else if (expr is VariableDeclarationStatement) {
resolveNode = ((VariableDeclarationStatement)expr).Type;
} else {
resolveNode = expr;
}
try {
var ctx = CSharpParsedFile.GetResolver(Compilation, location);
var root = expr.AncestorsAndSelf.FirstOrDefault(n => n is EntityDeclaration || n is CompilationUnit);
if (root == null) {
return null;
}
var csResolver = new CSharpAstResolver (ctx, root, CSharpParsedFile);
var result = csResolver.Resolve(resolveNode);
var state = csResolver.GetResolverStateBefore(resolveNode);
return Tuple.Create(result, state);
} catch (Exception e) {
Console.WriteLine(e);
return null;
}
}
示例10: ResolveExpression
protected Tuple<ResolveResult, CSharpResolver> ResolveExpression (CSharpParsedFile file, AstNode expr, CompilationUnit unit)
{
if (expr == null)
return null;
AstNode resolveNode;
if (expr is Expression || expr is AstType) {
resolveNode = expr;
} else if (expr is VariableDeclarationStatement) {
resolveNode = ((VariableDeclarationStatement)expr).Type;
} else {
resolveNode = expr;
}
// var newContent = ProjectContent.UpdateProjectContent (CSharpParsedFile, file);
var csResolver = new CSharpAstResolver(new CSharpResolver (ctx), unit, CSharpParsedFile);
var result = csResolver.Resolve (resolveNode);
var state = csResolver.GetResolverStateBefore (resolveNode);
return Tuple.Create (result, state);
}
示例11: GetLanguageItem
public ResolveResult GetLanguageItem (MonoDevelop.Ide.Gui.Document doc, int offset, string expression)
{
if (offset < 0) {
return null;
}
var parsedDocument = doc.ParsedDocument;
if (parsedDocument == null)
return null;
var data = doc.Editor;
var loc = data.OffsetToLocation (offset);
var unit = parsedDocument.GetAst<SyntaxTree> ();
var parsedFile = parsedDocument.ParsedFile as CSharpUnresolvedFile;
if (unit == null || parsedFile == null) {
return null;
}
var node = unit.GetNodeAt (loc);
if (node == null) {
return null;
}
var resolver = new CSharpAstResolver (doc.Compilation, unit, parsedFile);
resolver.ApplyNavigator (new NodeListResolveVisitorNavigator (node), CancellationToken.None);
var state = resolver.GetResolverStateBefore (node, CancellationToken.None);
var list = new List<IType> ();
int indexOf = expression.IndexOf ('`');
if (indexOf != -1) {
var intType = new PrimitiveType ("int").ToTypeReference ().Resolve (doc.Compilation);
var num = expression.Substring (indexOf + 1);
int number = int.Parse (num);
for (int i = 0; i < number; i++)
list.Add (intType);
expression = expression.Remove (indexOf);
}
return state.LookupSimpleNameOrTypeName (expression, list, NameLookupMode.Expression);
}