本文整理汇总了C#中MonoDevelop.CSharp.Resolver.NewCSharpExpressionFinder类的典型用法代码示例。如果您正苦于以下问题:C# NewCSharpExpressionFinder类的具体用法?C# NewCSharpExpressionFinder怎么用?C# NewCSharpExpressionFinder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NewCSharpExpressionFinder类属于MonoDevelop.CSharp.Resolver命名空间,在下文中一共展示了NewCSharpExpressionFinder类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateCtrlSpaceCompletionData
CompletionDataList CreateCtrlSpaceCompletionData (CodeCompletionContext ctx, ExpressionResult expressionResult)
{
NRefactoryResolver resolver = CreateResolver ();
DomLocation cursorLocation = new DomLocation (ctx.TriggerLine, ctx.TriggerLineOffset);
resolver.SetupResolver (cursorLocation);
CompletionDataList result = new ProjectDomCompletionDataList ();
CompletionDataCollector col = new CompletionDataCollector (dom, result, Document.CompilationUnit, resolver.CallingType, cursorLocation);
if (expressionResult == null) {
AddPrimitiveTypes (col);
resolver.AddAccessibleCodeCompletionData (ExpressionContext.Global, col);
} else if (expressionResult.ExpressionContext == ExpressionContext.TypeDeclaration) {
AddPrimitiveTypes (col);
AddNRefactoryKeywords (col, ICSharpCode.NRefactory.Parser.CSharp.Tokens.TypeLevel);
resolver.AddAccessibleCodeCompletionData (expressionResult.ExpressionContext, col);
} else if (expressionResult.ExpressionContext == ExpressionContext.InterfaceDeclaration) {
AddPrimitiveTypes (col);
AddNRefactoryKeywords (col, ICSharpCode.NRefactory.Parser.CSharp.Tokens.InterfaceLevel);
resolver.AddAccessibleCodeCompletionData (expressionResult.ExpressionContext, col);
} else if (expressionResult.ExpressionContext == ExpressionContext.MethodBody) {
col.Add ("global", "md-keyword");
col.Add ("var", "md-keyword");
AddNRefactoryKeywords (col, ICSharpCode.NRefactory.Parser.CSharp.Tokens.StatementStart);
AddPrimitiveTypes (col);
resolver.AddAccessibleCodeCompletionData (expressionResult.ExpressionContext, col);
} else if (expressionResult.ExpressionContext == ExpressionContext.InterfacePropertyDeclaration) {
col.Add ("get", "md-keyword");
col.Add ("set", "md-keyword");
} else if (expressionResult.ExpressionContext == ExpressionContext.ConstraintsStart) {
col.Add ("where", "md-keyword");
} else if (expressionResult.ExpressionContext == ExpressionContext.Constraints) {
col.Add ("new", "md-keyword");
col.Add ("class", "md-keyword");
col.Add ("struct", "md-keyword");
AddPrimitiveTypes (col);
resolver.AddAccessibleCodeCompletionData (expressionResult.ExpressionContext, col);
} else if (expressionResult.ExpressionContext == ExpressionContext.Attribute) {
col.Add ("assembly", "md-keyword");
col.Add ("module", "md-keyword");
col.Add ("type", "md-keyword");
col.Add ("method", "md-keyword");
col.Add ("field", "md-keyword");
col.Add ("property", "md-keyword");
col.Add ("event", "md-keyword");
col.Add ("param", "md-keyword");
col.Add ("return", "md-keyword");
resolver.AddAccessibleCodeCompletionData (expressionResult.ExpressionContext, col);
} else if (expressionResult.ExpressionContext == ExpressionContext.BaseConstructorCall) {
col.Add ("this", "md-keyword");
col.Add ("base", "md-keyword");
} else if (expressionResult.ExpressionContext == ExpressionContext.ParameterType || expressionResult.ExpressionContext == ExpressionContext.FirstParameterType) {
col.Add ("ref", "md-keyword");
col.Add ("out", "md-keyword");
col.Add ("params", "md-keyword");
// C# 3.0 extension method
if (expressionResult.ExpressionContext == ExpressionContext.FirstParameterType)
col.Add ("this", "md-keyword");
AddPrimitiveTypes (col);
resolver.AddAccessibleCodeCompletionData (expressionResult.ExpressionContext, col);
} else if (expressionResult.ExpressionContext == ExpressionContext.PropertyDeclaration) {
AddNRefactoryKeywords (col, ICSharpCode.NRefactory.Parser.CSharp.Tokens.InPropertyDeclaration);
} else if (expressionResult.ExpressionContext == ExpressionContext.EventDeclaration) {
col.Add ("add", "md-keyword");
col.Add ("remove", "md-keyword");
} //else if (expressionResult.ExpressionContext == ExpressionContext.FullyQualifiedType) {}
else if (expressionResult.ExpressionContext == ExpressionContext.Default) {
col.Add ("global", "md-keyword");
col.Add ("var", "md-keyword");
AddPrimitiveTypes (col);
AddNRefactoryKeywords (col, ICSharpCode.NRefactory.Parser.CSharp.Tokens.ExpressionStart);
AddNRefactoryKeywords (col, ICSharpCode.NRefactory.Parser.CSharp.Tokens.ExpressionContent);
resolver.AddAccessibleCodeCompletionData (expressionResult.ExpressionContext, col);
} else if (expressionResult.ExpressionContext == ExpressionContext.Global) {
AddNRefactoryKeywords (col, ICSharpCode.NRefactory.Parser.CSharp.Tokens.GlobalLevel);
CodeTemplateService.AddCompletionDataForMime ("text/x-csharp", result);
} else if (expressionResult.ExpressionContext == ExpressionContext.ObjectInitializer) {
ExpressionContext exactContext = new NewCSharpExpressionFinder (dom).FindExactContextForObjectInitializer (textEditorData, resolver.Unit, Document.FileName, resolver.CallingType);
if (exactContext is ExpressionContext.TypeExpressionContext) {
IReturnType objectInitializer = ((ExpressionContext.TypeExpressionContext)exactContext).UnresolvedType;
if (objectInitializer.ArrayDimensions > 0 || objectInitializer.PointerNestingLevel > 0) {
col.Add ("global", "md-keyword");
col.Add ("new", "md-keyword");
AddPrimitiveTypes (col);
resolver.AddAccessibleCodeCompletionData (expressionResult.ExpressionContext, col);
return result;
}
IType foundType = resolver.SearchType (objectInitializer);
if (foundType == null)
foundType = dom.GetType (objectInitializer);
if (foundType != null) {
bool includeProtected = DomType.IncludeProtected (dom, foundType, resolver.CallingType);
foreach (IType type in dom.GetInheritanceTree (foundType)) {
foreach (IProperty property in type.Properties) {
if (property.IsAccessibleFrom (dom, resolver.CallingType, resolver.CallingMember, includeProtected)) {
col.Add (property);
}
}
foreach (var field in type.Fields) {
//.........这里部分代码省略.........
示例2: HandleKeywordCompletion
public ICompletionDataList HandleKeywordCompletion (CodeCompletionContext completionContext, ExpressionResult result, int wordStart, string word)
{
if (stateTracker.Engine.IsInsideDocLineComment || stateTracker.Engine.IsInsideOrdinaryCommentOrString)
return null;
DomLocation location = new DomLocation (completionContext.TriggerLine, completionContext.TriggerLineOffset);
switch (word) {
case "using":
if (result.ExpressionContext != ExpressionContext.NamespaceNameExcepted)
return null;
return CreateCompletionData (location, new NamespaceResolveResult (""), result, null);
case "namespace":
result.ExpressionContext = ExpressionContext.NamespaceNameExcepted;
return CreateCompletionData (location, new NamespaceResolveResult (""), result, null);
case "case":
return CreateCaseCompletionData (location, result);
case ",":
case ":":
if (result.ExpressionContext == ExpressionContext.InheritableType) {
IType cls = NRefactoryResolver.GetTypeAtCursor (Document.CompilationUnit, Document.FileName, new DomLocation (completionContext.TriggerLine, completionContext.TriggerLineOffset));
CompletionDataList completionList = new ProjectDomCompletionDataList ();
List<string> namespaceList = GetUsedNamespaces ();
CSharpTextEditorCompletion.CompletionDataCollector col = new CSharpTextEditorCompletion.CompletionDataCollector (dom, completionList, Document.CompilationUnit, null, location);
bool isInterface = false;
HashSet<string> baseTypeNames = new HashSet<string> ();
if (cls != null) {
baseTypeNames.Add (cls.Name);
if (cls.ClassType == ClassType.Struct)
isInterface = true;
}
int tokenIndex = completionContext.TriggerOffset;
// Search base types " : [Type1, ... ,TypeN,] <Caret>"
string token = null;
do {
token = GetPreviousToken (ref tokenIndex, false);
if (string.IsNullOrEmpty (token))
break;
token = token.Trim ();
if (Char.IsLetterOrDigit (token[0]) || token[0] == '_') {
IType baseType = dom.SearchType (Document.CompilationUnit, Document.CompilationUnit, token);
if (baseType != null) {
if (baseType.ClassType != ClassType.Interface)
isInterface = true;
baseTypeNames.Add (baseType.Name);
}
}
} while (token != ":");
foreach (object o in dom.GetNamespaceContents (namespaceList, true, true)) {
IType type = o as IType;
if (type != null && (type.IsStatic || type.IsSealed || baseTypeNames.Contains (type.Name) || isInterface && type.ClassType != ClassType.Interface)) {
continue;
}
if (o is Namespace && !namespaceList.Any (ns => ns.StartsWith (((Namespace)o).FullName)))
continue;
col.Add (o);
}
// Add inner classes
Stack<IType> innerStack = new Stack<IType> ();
innerStack.Push (cls);
while (innerStack.Count > 0) {
IType curType = innerStack.Pop ();
if (curType == null)
continue;
foreach (IType innerType in curType.InnerTypes) {
if (innerType != cls)
// don't add the calling class as possible base type
col.Add (innerType);
}
if (curType.DeclaringType != null)
innerStack.Push (curType.DeclaringType);
}
return completionList;
}
break;
case "is":
case "as":
{
CompletionDataList completionList = new ProjectDomCompletionDataList ();
ExpressionResult expressionResult = FindExpression (dom, completionContext, wordStart - textEditorData.Caret.Offset);
NRefactoryResolver resolver = CreateResolver ();
ResolveResult resolveResult = resolver.Resolve (expressionResult, new DomLocation (completionContext.TriggerLine, completionContext.TriggerLineOffset));
if (resolveResult != null && resolveResult.ResolvedType != null) {
CompletionDataCollector col = new CompletionDataCollector (dom, completionList, Document.CompilationUnit, resolver.CallingType, location);
IType foundType = null;
if (word == "as") {
ExpressionContext exactContext = new NewCSharpExpressionFinder (dom).FindExactContextForAsCompletion (textEditorData, Document.CompilationUnit, Document.FileName, resolver.CallingType);
if (exactContext is ExpressionContext.TypeExpressionContext) {
foundType = resolver.SearchType (((ExpressionContext.TypeExpressionContext)exactContext).Type);
AddAsCompletionData (col, foundType);
}
}
if (foundType == null)
foundType = resolver.SearchType (resolveResult.ResolvedType);
if (foundType != null) {
if (foundType.ClassType == ClassType.Interface)
foundType = resolver.SearchType (DomReturnType.Object);
//.........这里部分代码省略.........
示例3: HandleCodeCompletion
//.........这里部分代码省略.........
textEditorData.Insert (cursor, generatedComment.ToString ());
textEditorData.Caret.Offset = cursor + newCursorOffset;
return null;
}
return null;
// case '\n':
// case '\r': {
// if (stateTracker.Engine.IsInsideDocLineComment || stateTracker.Engine.IsInsideOrdinaryCommentOrString)
// return null;
// result = FindExpression (dom, completionContext);
// if (result == null)
// return null;
//
//
// int tokenIndex = completionContext.TriggerOffset;
// string token = GetPreviousToken (ref tokenIndex, false);
// if (result.ExpressionContext == ExpressionContext.ObjectInitializer) {
// if (token == "{" || token == ",")
// return CreateCtrlSpaceCompletionData (completionContext, result);
// }
// return null;
// }
case ' ':
if (stateTracker.Engine.IsInsideDocLineComment || stateTracker.Engine.IsInsideOrdinaryCommentOrString)
return null;
result = FindExpression (dom, completionContext);
if (result == null)
return null;
int tokenIndex = completionContext.TriggerOffset;
string token = GetPreviousToken (ref tokenIndex, false);
if (result.ExpressionContext == ExpressionContext.ObjectInitializer) {
resolver = CreateResolver ();
ExpressionContext exactContext = new NewCSharpExpressionFinder (dom).FindExactContextForObjectInitializer (textEditorData, resolver.Unit, Document.FileName, resolver.CallingType);
IReturnType objectInitializer = ((ExpressionContext.TypeExpressionContext)exactContext).UnresolvedType;
if (objectInitializer != null && objectInitializer.ArrayDimensions == 0 && objectInitializer.PointerNestingLevel == 0 && (token == "{" || token == ","))
return CreateCtrlSpaceCompletionData (completionContext, result);
}
if (token == "=") {
int j = tokenIndex;
string prevToken = GetPreviousToken (ref j, false);
if (prevToken == "=" || prevToken == "+" || prevToken == "-") {
token = prevToken + token;
tokenIndex = j;
}
}
switch (token) {
case "=":
case "==":
result = FindExpression (dom, completionContext, tokenIndex - completionContext.TriggerOffset - 1);
resolver = CreateResolver ();
resolveResult = resolver.Resolve (result, location);
if (resolveResult != null) {
IType resolvedType = dom.GetType (resolveResult.ResolvedType);
if (resolvedType == null)
return null;
if (resolvedType.ClassType == ClassType.Enum) {
CompletionDataList completionList = new ProjectDomCompletionDataList ();
CompletionDataCollector cdc = new CompletionDataCollector (dom, completionList, Document.CompilationUnit, resolver.CallingType, location);
IReturnType returnType = new DomReturnType (resolvedType);
bool added = false;
foreach (IUsing u in Document.CompilationUnit.Usings) {
foreach (KeyValuePair<string, IReturnType> alias in u.Aliases) {
if (alias.Value.ToInvariantString () == returnType.ToInvariantString ()) {
cdc.Add (alias.Key, "md-class");
added = true;
示例4: FindExpression
ExpressionResult FindExpression (ProjectDom dom, CodeCompletionContext ctx)
{
NewCSharpExpressionFinder expressionFinder = new NewCSharpExpressionFinder (dom);
try {
return expressionFinder.FindExpression (textEditorData, ctx.TriggerOffset);
} catch (Exception ex) {
LoggingService.LogWarning (ex.Message, ex);
return null;
}
}
示例5: FindExpression
ExpressionResult FindExpression (ProjectDom dom, CodeCompletionContext ctx, int offset)
{
NewCSharpExpressionFinder expressionFinder = new NewCSharpExpressionFinder (dom);
try {
return expressionFinder.FindExpression (Editor.Text, Math.Max (ctx.TriggerOffset + offset, 0));
} catch (Exception ex) {
LoggingService.LogWarning (ex.Message, ex);
return null;
}
}
示例6: GetLanguageItem
public MonoDevelop.Projects.Dom.ResolveResult GetLanguageItem (ProjectDom dom, Mono.TextEditor.TextEditorData data, int offset)
{
if (offset < 0)
return null;
string fileName = data.Document.FileName;
IParser parser = ProjectDomService.GetParser (fileName);
if (parser == null)
return null;
MonoDevelop.Ide.Gui.Document doc = IdeApp.Workbench.ActiveDocument;
if (doc == null)
return null;
IResolver resolver = parser.CreateResolver (dom, doc, fileName);
if (resolver == null)
return null;
var expressionFinder = new NewCSharpExpressionFinder (dom);
int wordEnd = Math.Min (offset, data.Length - 1);
if (data.GetCharAt (wordEnd) == '@')
wordEnd++;
while (wordEnd < data.Length && (Char.IsLetterOrDigit (data.GetCharAt (wordEnd)) || data.GetCharAt (wordEnd) == '_'))
wordEnd++;
while (wordEnd < data.Length - 1 && Char.IsWhiteSpace (data.GetCharAt (wordEnd)))
wordEnd++;
/* is checked at the end.
int saveEnd = wordEnd;
if (wordEnd < data.Length && data.GetCharAt (wordEnd) == '<') {
int matchingBracket = data.Document.GetMatchingBracketOffset (wordEnd);
if (matchingBracket > 0)
wordEnd = matchingBracket;
while (wordEnd < data.Length - 1 && Char.IsWhiteSpace (data.GetCharAt (wordEnd)))
wordEnd++;
}
bool wasMethodCall = false;
if (data.GetCharAt (wordEnd) == '(') {
int matchingBracket = data.Document.GetMatchingBracketOffset (wordEnd);
if (matchingBracket > 0) {
wordEnd = matchingBracket;
wasMethodCall = true;
}
}
if (!wasMethodCall)
wordEnd = saveEnd;*/
ExpressionResult expressionResult = expressionFinder.FindExpression (data, wordEnd);
if (expressionResult == null)
return null;
ResolveResult resolveResult;
DocumentLocation loc = data.Document.OffsetToLocation (offset);
string savedExpression = null;
// special handling for 'var' "keyword"
if (expressionResult.ExpressionContext == ExpressionContext.IdentifierExpected && expressionResult.Expression != null && expressionResult.Expression.Trim () == "var") {
int endOffset = data.Document.LocationToOffset (expressionResult.Region.End.Line, expressionResult.Region.End.Column);
StringBuilder identifer = new StringBuilder ();
for (int i = endOffset; i >= 0 && i < data.Document.Length; i++) {
char ch = data.Document.GetCharAt (i);
if (Char.IsWhiteSpace (ch))
continue;
if (ch == '=')
break;
if (Char.IsLetterOrDigit (ch) || ch =='_') {
identifer.Append (ch);
continue;
}
identifer.Length = 0;
break;
}
if (identifer.Length > 0) {
expressionResult.Expression = identifer.ToString ();
resolveResult = resolver.Resolve (expressionResult, new DomLocation (loc.Line, loc.Column));
if (resolveResult != null) {
resolveResult = new MemberResolveResult (dom.GetType (resolveResult.ResolvedType));
resolveResult.ResolvedExpression = expressionResult;
return resolveResult;
}
}
}
if (expressionResult.ExpressionContext == ExpressionContext.Attribute) {
savedExpression = expressionResult.Expression;
expressionResult.Expression = expressionResult.Expression.Trim () + "Attribute";
expressionResult.ExpressionContext = ExpressionContext.ObjectCreation;
}
resolveResult = resolver.Resolve (expressionResult, new DomLocation (loc.Line, loc.Column));
if (savedExpression != null && resolveResult == null) {
expressionResult.Expression = savedExpression;
resolveResult = resolver.Resolve (expressionResult, new DomLocation (loc.Line, loc.Column));
}
// Search for possible generic parameters.
// if (this.resolveResult == null || this.resolveResult.ResolvedType == null || String.IsNullOrEmpty (this.resolveResult.ResolvedType.Name)) {
if (!expressionResult.Region.IsEmpty) {
int j = data.Document.LocationToOffset (expressionResult.Region.End.Line, expressionResult.Region.End.Column);
int bracket = 0;
for (int i = j; i >= 0 && i < data.Document.Length; i++) {
char ch = data.Document.GetCharAt (i);
if (Char.IsWhiteSpace (ch))
//.........这里部分代码省略.........
示例7: CreateRefactoringOptions
internal static RefactoringOptions CreateRefactoringOptions (string text)
{
int cursorPosition = -1;
int endPos = text.IndexOf ('$');
if (endPos >= 0) {
cursorPosition = endPos;
text = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1);
}
int selectionStart = -1;
int selectionEnd = -1;
int idx = text.IndexOf ("<-");
if (idx >= 0) {
selectionStart = idx;
text = text.Substring (0, idx) + text.Substring (idx + 2);
selectionEnd = idx = text.IndexOf ("->");
text = text.Substring (0, idx) + text.Substring (idx + 2);
if (cursorPosition < 0)
cursorPosition = selectionEnd - 1;
}
TestWorkbenchWindow tww = new TestWorkbenchWindow ();
TestViewContent sev = new TestViewContent ();
// return new RefactoringOptions ();
DotNetProject project = new DotNetAssemblyProject ("C#");
Solution solution = new Solution ();
solution.RootFolder.Items.Add (project);
project.FileName = GetTempFile (".csproj");
string file = GetTempFile (".cs");
project.AddFile (file);
string parsedText = text;
string editorText = text;
ProjectDomService.Load (project);
ProjectDom dom = ProjectDomService.GetProjectDom (project);
dom.ForceUpdate (true);
ProjectDomService.Parse (project, file, delegate { return parsedText; });
ProjectDomService.Parse (project, file, delegate { return parsedText; });
sev.Project = project;
sev.ContentName = file;
sev.Text = editorText;
sev.CursorPosition = cursorPosition;
tww.ViewContent = sev;
var doc = new MonoDevelop.Ide.Gui.Document (tww);
doc.Editor.Document.MimeType = "text/x-csharp";
doc.Editor.Document.FileName = file;
doc.ParsedDocument = new McsParser ().Parse (null, sev.ContentName, parsedText);
foreach (var e in doc.ParsedDocument.Errors)
Console.WriteLine (e);
if (cursorPosition >= 0)
doc.Editor.Caret.Offset = cursorPosition;
if (selectionStart >= 0)
doc.Editor.SetSelection (selectionStart, selectionEnd);
NRefactoryResolver resolver = new NRefactoryResolver (dom,
doc.ParsedDocument.CompilationUnit,
sev.Data,
file);
ExpressionResult expressionResult;
if (selectionStart >= 0) {
expressionResult = new ExpressionResult (editorText.Substring (selectionStart, selectionEnd - selectionStart).Trim ());
endPos = selectionEnd;
} else {
expressionResult = new NewCSharpExpressionFinder (dom).FindFullExpression (doc.Editor, cursorPosition + 1);
}
ResolveResult resolveResult = endPos >= 0 ? resolver.Resolve (expressionResult, new DomLocation (doc.Editor.Caret.Line, doc.Editor.Caret.Column)) : null;
RefactoringOptions result = new RefactoringOptions {
Document = doc,
Dom = dom,
ResolveResult = resolveResult,
SelectedItem = null
};
if (resolveResult is MemberResolveResult)
result.SelectedItem = ((MemberResolveResult)resolveResult).ResolvedMember;
if (resolveResult is LocalVariableResolveResult)
result.SelectedItem = ((LocalVariableResolveResult)resolveResult).LocalVariable;
if (resolveResult is ParameterResolveResult)
result.SelectedItem = ((ParameterResolveResult)resolveResult).Parameter;
result.TestFileProvider = new FileProvider (result);
return result;
}