public ExpressionResult(string expression, DomRegion region, ExpressionContext context, object tag)
		{
			this.Expression = expression;
			this.Region = region;
			this.Context = context;
			this.Tag = tag;
		}
		void FindFull(string program, string location, string expectedExpression, ExpressionContext expectedContext)
		{
			int pos = program.IndexOf(location);
			if (pos < 0) Assert.Fail("location not found in program");
			ExpressionResult er = ef.FindFullExpression(program, pos);
			Assert.AreEqual(expectedExpression, er.Expression);
			Assert.AreEqual(expectedContext.ToString(), er.Context.ToString());
		}
示例#3
0
		List<ICompletionEntry> CtrlSpace(PythonResolverContext resolverContext, ExpressionContext expressionContext)
		{
			if (resolverContext.HasProjectContent) {
				if (expressionContext == ExpressionContext.Namespace) {
					return GetImportCompletionItems(resolverContext.ProjectContent);
				}
				return resolverContext.GetImportedTypes();
			}
			return new List<ICompletionEntry>();
		}
示例#4
0
		/// <summary>
		/// Called when Ctrl+Space is entered by the user.
		/// </summary>
		public List<ICompletionEntry> CtrlSpace(int caretLine, int caretColumn, ParseInformation parseInfo, string fileContent, ExpressionContext context)
		{
			resolverContext = new PythonResolverContext(parseInfo);
			if (resolverContext.HasProjectContent) {
				if (context == ExpressionContext.Namespace) {
					PythonImportCompletion importCompletion = new PythonImportCompletion(resolverContext.ProjectContent);
					return importCompletion.GetCompletionItems();
				} else {
					return resolverContext.GetImportedTypes();
				}
			}
			return new List<ICompletionEntry>();
		}
		/// <summary>
		/// Resolves an expression using low-level NRefactoryResolver methods and making
		/// use of the cache if possible.
		/// </summary>
		/// <param name="fileName">The file name of the source code file that contains the expression to be resolved.</param>
		/// <param name="fileContent">The content of the source code file that contains the expression to be resolved.</param>
		/// <param name="caretLine">The 1-based line number of the expression.</param>
		/// <param name="caretColumn">The 1-based column number of the expression.</param>
		/// <param name="compilationUnit">The CompilationUnit that contains the NRefactory AST for this file. May be <c>null</c> (then the CompilationUnit is retrieved from the cache or the file is parsed).</param>
		/// <param name="expression">The expression to be resolved.</param>
		/// <param name="context">The ExpressionContext of the expression.</param>
		/// <returns>A ResolveResult or <c>null</c> if the expression cannot be resolved.</returns>
		public static ResolveResult ResolveLowLevel(string fileName, string fileContent, int caretLine, int caretColumn, CompilationUnit compilationUnit, string expression, ExpressionContext context)
		{
			Expression expr = ParseExpression(fileName, expression, caretLine, caretColumn);
			if (expr == null) return null;
			return ResolveLowLevel(fileName, fileContent, caretLine, caretColumn, compilationUnit, expression, expr, context);
		}
示例#6
0
		public BooCtrlSpaceCompletionItemProvider(ExpressionContext overrideContext) : base(overrideContext) {}
			public CombinedExpressionContext(byte opType, ExpressionContext a, ExpressionContext b)
			{
				if (a == null)
					throw new ArgumentNullException("a");
				if (b == null)
					throw new ArgumentNullException("a");
				this.opType = opType;
				this.a = a;
				this.b = b;
			}
		public static ICompletionItem CreateCompletionItem(object o, ExpressionContext context)
		{
			IEntity entity = o as IEntity;
			if (entity != null) {
				return new CodeCompletionItem(entity);
			} else if (o is Dom.NRefactoryResolver.KeywordEntry) {
				return new KeywordCompletionItem(o.ToString());
			} else {
				DefaultCompletionItem item = new DefaultCompletionItem(o.ToString());
				if (o is NamespaceEntry)
					item.Image = ClassBrowserIconService.Namespace;
				return item;
			}
		}
		public virtual ICompletionItemList GenerateCompletionListForCompletionData(List<ICompletionEntry> arr, ExpressionContext context)
		{
			var list = ConvertCompletionData(CreateCompletionItemList(), arr, context);
			InitializeCompletionItemList(list);
			
			return list;
		}
		protected void AddResolveResults(ResolveResult results, ExpressionContext context)
		{
			insertedElements.Clear();
			insertedPropertiesElements.Clear();
			insertedEventElements.Clear();
			
			if (results != null) {
				AddResolveResults(results.GetCompletionData(ParserService.CurrentProjectContent), context);
			}
		}
		ToolStripMenuItem MakeItemForResolveError(UnknownIdentifierResolveResult rr, ExpressionContext context, TextArea textArea)
		{
			return MakeItemForUnknownClass(rr.CallingClass, rr.Identifier, textArea);
		}
示例#12
0
		/// <summary>
		/// Called when Ctrl+Space is entered by the user.
		/// </summary>
		public List<ICompletionEntry> CtrlSpace(int caretLine, int caretColumn, ParseInformation parseInfo, string fileContent, ExpressionContext context)
		{
			resolverContext = new PythonResolverContext(parseInfo, fileContent);
			return CtrlSpace(resolverContext, context);
		}
		public AttributesDataProvider(ExpressionContext context) : base(context)
		{
		}
		public AttributesDataProvider(ExpressionContext context) : base(context)
		{
			this.ForceNewExpression = true;
		}
		ToolStripMenuItem MakeItemForResolveError(UnknownConstructorCallResolveResult rr, ExpressionContext context, TextArea textArea)
		{
			return MakeItemForUnknownClass(rr.CallingClass, rr.TypeName, textArea);
		}
		CodeCompletionData CreateItem(object o, ExpressionContext context)
		{
			if (o is string) {
				return new CodeCompletionData(o.ToString(), ClassBrowserIconService.NamespaceIndex);
			} else if (o is IClass) {
				return new CodeCompletionData((IClass)o);
			} else if (o is IProperty) {
				IProperty property = (IProperty)o;
				if (property.Name != null && insertedPropertiesElements[property.Name] == null) {
					insertedPropertiesElements[property.Name] = property;
					return new CodeCompletionData(property);
				}
			} else if (o is IMethod) {
				IMethod method = (IMethod)o;
				if (method.Name != null) {
					CodeCompletionData ccd = new CodeCompletionData(method);
					if (insertedElements[method.Name] == null) {
						insertedElements[method.Name] = ccd;
						return ccd;
					} else {
						CodeCompletionData oldMethod = (CodeCompletionData)insertedElements[method.Name];
						++oldMethod.Overloads;
					}
				}
			} else if (o is IField) {
				return new CodeCompletionData((IField)o);
			} else if (o is IEvent) {
				IEvent e = (IEvent)o;
				if (e.Name != null && insertedEventElements[e.Name] == null) {
					insertedEventElements[e.Name] = e;
					return new CodeCompletionData(e);
				}
			} else {
				throw new ApplicationException("Unknown object: " + o);
			}
			return null;
		}
		ExpressionResult CreateExpressionResult(string text, ExpressionRange range, ExpressionContext context)
		{
			string expression = Substring(text, range);
			return new ExpressionResult(expression, context);
		}
		public ExpressionResult(string expression, ExpressionContext context) : this(expression, DomRegion.Empty, context, null) {}
		public ExpressionResult(string expression, ExpressionContext context) : this(expression, context, null) {}
		public virtual ICompletionItemList GenerateCompletionListForResolveResult(ResolveResult rr, ExpressionContext context)
		{
			if (rr == null)
				return null;
			IProjectContent callingContent = rr.CallingClass != null ? rr.CallingClass.ProjectContent : null;
			List<ICompletionEntry> arr = rr.GetCompletionData(callingContent ?? ParserService.CurrentProjectContent, this.ShowItemsFromAllNamespaces);
			return GenerateCompletionListForCompletionData(arr, context);
		}
		/// <summary>
		/// Resolves an expression in the current node's context.
		/// </summary>
		/// <param name="expression">The expression to be resolved.</param>
		/// <param name="context">The ExpressionContext.</param>
		public ResolveResult Resolve(Expression expression, ExpressionContext context)
		{
			if (!this.PositionAvailable) {
				LoggingService.Info("ResourceToolkit: PositionTrackingAstVisitor: Resolve failed due to position information being unavailable. Expression: "+expression.ToString());
				return null;
			}
			
			#if DEBUG
			LoggingService.Debug("ResourceToolkit: PositionTrackingAstVisitor: Using this parent node for resolve: "+this.parentNodes.Peek().ToString());
			#endif
			
			return NRefactoryAstCacheService.ResolveLowLevel(this.fileName, this.fileContent, this.CurrentNodeStartLocation.Y, this.CurrentNodeStartLocation.X+1, this.compilationUnit, null, expression, context);
		}
		public static DefaultCompletionItemList ConvertCompletionData(DefaultCompletionItemList result, List<ICompletionEntry> arr, ExpressionContext context)
		{
			if (arr == null)
				return result;
			
			Dictionary<string, CodeCompletionItem> methodItems = new Dictionary<string, CodeCompletionItem>();
			foreach (ICompletionEntry o in arr) {
				if (context != null && !context.ShowEntry(o))
					continue;
				
				IMethod method = o as IMethod;
				if (method != null) {
					CodeCompletionItem codeItem;
					if (methodItems.TryGetValue(method.Name, out codeItem)) {
						codeItem.Overloads++;
						continue;
					}
				}
				
				ICompletionItem item = CreateCompletionItem(o, context);
				if (item != null) {
					result.Items.Add(item);
					CodeCompletionItem codeItem = item as CodeCompletionItem;
					if (method != null && codeItem != null) {
						methodItems[method.Name] = codeItem;
					}
				}
			}
			
			// Suggested entry (List<int> a = new => suggest List<int>).
			if (context.SuggestedItem is SuggestedCodeCompletionItem) {
				result.SuggestedItem = (SuggestedCodeCompletionItem)context.SuggestedItem;
				result.Items.Insert(0, result.SuggestedItem);
			}
			return result;
		}
示例#23
0
		////////////////////////////////////
		
		public static ArrayList CtrlSpace(int caretLine, int caretColumn, string fileName, string fileContent, ExpressionContext context)
		{
			IResolver resolver = CreateResolver(fileName);
			if (resolver != null) {
				return resolver.CtrlSpace(caretLine, caretColumn, fileName, fileContent, context);
			}
			return null;
		}
		void ContextTest(string program, ExpressionContext context)
		{
			int pos = program.IndexOf("|");
			if (pos < 0) Assert.Fail("location not found in program");
			program = program.Remove(pos, 1);
			ExpressionResult er = ef.FindExpression(program, pos);
			Assert.AreEqual(context.ToString(), er.Context.ToString());
		}
示例#25
0
        void AddCompletionData(ref List <ICompletionData> resultList, ArrayList completionData, Dom.ExpressionContext context)
        {
            // used to store the method names for grouping overloads
            Dictionary <string, CSCompletionData> nameDictionary = new Dictionary <string, CSCompletionData>();

            // Add the completion data as returned by SharpDevelop.Dom to the
            // list for the text editor
            foreach (object obj in completionData)
            {
                if (!context.ShowEntry(obj))
                {
                    continue;
                }

                if (obj is string)
                {
                    // namespace names are returned as string
                    resultList.Add(new DefaultCompletionData((string)obj, "namespace " + obj, 5));
                }
                else if (obj is Dom.IClass)
                {
                    Dom.IClass c = (Dom.IClass)obj;
                    resultList.Add(new CSCompletionData(c));
                }
                else if (obj is Dom.IMember)
                {
                    Dom.IMember m = (Dom.IMember)obj;
                    if (m is Dom.IMethod && ((m as Dom.IMethod).IsConstructor))
                    {
                        // Skip constructors
                        continue;
                    }
                    // Group results by name and add "(x Overloads)" to the
                    // description if there are multiple results with the same name.

                    CSCompletionData data;
                    if (nameDictionary.TryGetValue(m.Name, out data))
                    {
                        data.AddOverload();
                    }
                    else
                    {
                        nameDictionary[m.Name] = data = new CSCompletionData(m);
                        resultList.Add(data);
                    }
                }
                else
                {
                    // Current ICSharpCode.SharpDevelop.Dom should never return anything else
                    throw new NotSupportedException();
                }
            }
        }
示例#26
0
		public List<ICompletionEntry> CtrlSpace(int caretLine, int caretColumn, ParseInformation parseInfo, string fileContent, ExpressionContext context)
		{
			List<ICompletionEntry> result = new List<ICompletionEntry>();
			
			if (!Initialize(parseInfo, caretLine, caretColumn))
				return null;
			if (context == ExpressionContext.Importable) {
				pc.AddNamespaceContents(result, "", pc.Language, true);
				CtrlSpaceResolveHelper.AddUsing(result, pc.DefaultImports, pc);
				return result;
			}
			
			CtrlSpaceResolveHelper.AddContentsFromCalling(result, callingClass, callingMember);
			AddImportedNamespaceContents(result);
			
			if (BooProject.BooCompilerPC != null) {
				if (context == ExpressionFinder.BooAttributeContext.Instance) {
					foreach (object o in BooProject.BooCompilerPC.GetNamespaceContents("Boo.Lang")) {
						IClass c = o as IClass;
						if (c != null && c.Name.EndsWith("Attribute") && !c.IsAbstract) {
							result.Add(GetPrimitiveClass(BooProject.BooCompilerPC, c.FullyQualifiedName, c.Name.Substring(0, c.Name.Length - 9).ToLowerInvariant()));
						}
					}
				} else {
					foreach (object o in BooProject.BooCompilerPC.GetNamespaceContents("Boo.Lang")) {
						IClass c = o as IClass;
						if (c != null && c.Name.EndsWith("Macro") && !c.IsAbstract) {
							result.Add(GetPrimitiveClass(BooProject.BooCompilerPC, c.FullyQualifiedName, c.Name.Substring(0, c.Name.Length - 5).ToLowerInvariant()));
						}
					}
				}
			}
			
			List<string> knownVariableNames = new List<string>();
			foreach (object o in result) {
				IMember m = o as IMember;
				if (m != null) {
					knownVariableNames.Add(m.Name);
				}
			}
			VariableListLookupVisitor vllv = new VariableListLookupVisitor(knownVariableNames, this);
			vllv.Visit(GetCurrentBooMethod());
			foreach (KeyValuePair<string, IReturnType> entry in vllv.Results) {
				result.Add(new DefaultField.LocalVariableField(entry.Value, entry.Key, DomRegion.Empty, callingClass));
			}
			
			return result;
		}
		public CtrlSpaceCompletionDataProvider(ExpressionContext overrideContext)
		{
			this.overrideContext = overrideContext;
		}
示例#28
0
		protected override List<ICompletionEntry> CtrlSpace(ITextEditor editor, ExpressionContext context)
		{
			return new BooResolver().CtrlSpace(
				editor.Caret.Line,
				editor.Caret.Column,
				ParserService.GetParseInformation(editor.FileName),
				editor.Document.Text,
				context);
		}
		protected void AddResolveResults(ICollection list, ExpressionContext context)
		{
			if (list == null) {
				return;
			}
			completionData.Capacity += list.Count;
			CodeCompletionData suggestedData = null;
			foreach (object o in list) {
				if (context != null && !context.ShowEntry(o))
					continue;
				CodeCompletionData ccd = CreateItem(o, context);
				if (object.Equals(o, context.SuggestedItem))
					suggestedData = ccd;
				if (ccd != null)
					completionData.Add(ccd);
			}
			if (context.SuggestedItem != null) {
				if (suggestedData == null) {
					suggestedData = CreateItem(context.SuggestedItem, context);
					if (suggestedData != null) {
						completionData.Add(suggestedData);
					}
				}
				if (suggestedData != null) {
					completionData.Sort(DefaultCompletionData.Compare);
					this.DefaultIndex = completionData.IndexOf(suggestedData);
				}
			}
		}
		public static ResolveResult ResolveLowLevel(string fileName, string fileContent, int caretLine, int caretColumn, CompilationUnit compilationUnit, string expressionString, Expression expression, ExpressionContext context)
		{
			if (fileName == null) {
				throw new ArgumentNullException("fileName");
			}
			if (fileContent == null) {
				throw new ArgumentNullException("fileName");
			}
			if (expression == null) {
				throw new ArgumentNullException("expression");
			}
			
			IProject p = ProjectFileDictionaryService.GetProjectForFile(fileName);
			if (p == null) {
				LoggingService.Info("ResourceToolkit: NRefactoryAstCacheService: ResolveLowLevel failed. Project is null for file '"+fileName+"'");
				return null;
			}
			
			IProjectContent pc = ResourceResolverService.GetProjectContent(p);
			if (pc == null) {
				LoggingService.Info("ResourceToolkit: NRefactoryAstCacheService: ResolveLowLevel failed. ProjectContent is null for project '"+p.ToString()+"'");
				return null;
			}
			
			NRefactoryResolver resolver = ResourceResolverService.CreateResolver(fileName) as NRefactoryResolver;
			if (resolver == null) {
				resolver = new NRefactoryResolver(LanguageProperties.CSharp);
			}
			
			if (compilationUnit == null) {
				compilationUnit = GetFullAst(resolver.Language, fileName, fileContent);
			}
			if (compilationUnit == null) {
				LoggingService.Info("ResourceToolkit: NRefactoryAstCacheService: ResolveLowLevel failed due to the compilation unit being unavailable.");
				return null;
			}
			
			if (!resolver.Initialize(ParserService.GetParseInformation(fileName), caretLine, caretColumn)) {
				LoggingService.Info("ResourceToolkit: NRefactoryAstCacheService: ResolveLowLevel failed. NRefactoryResolver.Initialize returned false.");
				return null;
			}
			
			if (resolver.CallingClass != null) {
				ResolveResult rr;
				if (expressionString == null) {
					// HACK: Re-generate the code for the expression from the expression object by using the code generator.
					// This is necessary when invoking from inside an AST visitor where the
					// code belonging to this expression is unavailable.
					expressionString = resolver.LanguageProperties.CodeGenerator.GenerateCode(expression, String.Empty);
				}
				if ((rr = CtrlSpaceResolveHelper.GetResultFromDeclarationLine(resolver.CallingClass, resolver.CallingMember as IMethodOrProperty, caretLine, caretColumn, new ExpressionResult(expressionString))) != null) {
					return rr;
				}
			}
			
			if (resolver.CallingMember != null) {
				
				// Cache member->node mappings to improve performance
				// (if cache is enabled)
				INode memberNode;
				if (!CacheEnabled || !cachedMemberMappings.TryGetValue(resolver.CallingMember, out memberNode)) {
					MemberFindAstVisitor visitor = new MemberFindAstVisitor(resolver.CallingMember);
					compilationUnit.AcceptVisitor(visitor, null);
					memberNode = visitor.MemberNode;
					if (CacheEnabled && memberNode != null) {
						cachedMemberMappings.Add(resolver.CallingMember, memberNode);
					}
				}
				
				if (memberNode == null) {
					LoggingService.Info("ResourceToolkit: NRefactoryAstCacheService: Could not find member in AST: "+resolver.CallingMember.ToString());
				} else {
					resolver.RunLookupTableVisitor(memberNode);
				}
				
			}
			
			return resolver.ResolveInternal(expression, context);
		}
示例#31
0
		void FindFull(string location, string expectedExpression, ExpressionContext expectedContext)
		{
			FindFull(document, location, expectedExpression, expectedContext);
		}