From 10c5aadd928143ea8025122d3df1930639d55ccc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kon=C3=AD=C4=8Dek?= Date: Mon, 31 May 2010 18:05:47 +0000 Subject: [PATCH] CodeCompletion inserts Attributes without the "Attribute" suffix (correctly - only in attribute contexts, that is [*expr*] class *expr*). git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@5881 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../CodeCompletionItemProvider.cs | 21 ++++++++++++- .../Base/Project/Src/Util/ExtensionMethods.cs | 31 ++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/Main/Base/Project/Src/Editor/CodeCompletion/CodeCompletionItemProvider.cs b/src/Main/Base/Project/Src/Editor/CodeCompletion/CodeCompletionItemProvider.cs index 45aeeaadb5..886fa50278 100644 --- a/src/Main/Base/Project/Src/Editor/CodeCompletion/CodeCompletionItemProvider.cs +++ b/src/Main/Base/Project/Src/Editor/CodeCompletion/CodeCompletionItemProvider.cs @@ -11,8 +11,8 @@ using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; using System.Xml; - using ICSharpCode.Core; +using ICSharpCode.NRefactory; using ICSharpCode.SharpDevelop.Dom; using ICSharpCode.SharpDevelop.Project.Converter; using ICSharpCode.SharpDevelop.Refactoring; @@ -286,6 +286,11 @@ namespace ICSharpCode.SharpDevelop.Editor.CodeCompletion addUsing = true; } + // Special case for Attributes + if (insertedText.EndsWith("Attribute") && IsInAttributeContext(editor, context.StartOffset)) { + insertedText = insertedText.RemoveEnd("Attribute"); + } + // Insert the text context.Editor.Document.Replace(context.StartOffset, context.Length, insertedText); context.EndOffset = context.StartOffset + insertedText.Length; @@ -302,6 +307,20 @@ namespace ICSharpCode.SharpDevelop.Editor.CodeCompletion } } + /// + /// Returns true if the offset where we are inserting is in Attibute context, that is [*expr* + /// + bool IsInAttributeContext(ITextEditor editor, int offset) + { + if (editor == null || editor.Document == null) + return false; + var expressionFinder = ParserService.GetExpressionFinder(editor.FileName); + if (expressionFinder == null) + return false; + var resolvedExpression = expressionFinder.FindFullExpression(editor.Document.Text, offset); + return resolvedExpression.Context == ExpressionContext.Attribute; + } + #region Description string description; bool descriptionCreated; diff --git a/src/Main/Base/Project/Src/Util/ExtensionMethods.cs b/src/Main/Base/Project/Src/Util/ExtensionMethods.cs index 0eb1a3d3ee..876ac81930 100644 --- a/src/Main/Base/Project/Src/Util/ExtensionMethods.cs +++ b/src/Main/Base/Project/Src/Util/ExtensionMethods.cs @@ -17,7 +17,6 @@ using System.Windows.Controls; using System.Windows.Media; using System.Windows.Documents; using System.Windows.Forms; - using ICSharpCode.Core.Presentation; using ICSharpCode.SharpDevelop.Gui; using WinForms = System.Windows.Forms; @@ -319,6 +318,36 @@ namespace ICSharpCode.SharpDevelop } #endregion + /// + /// Removes from the start of this string. + /// Throws ArgumentException if this string does not start with . + /// + public static string RemoveStart(this string s, string stringToRemove) + { + if (s == null) + return null; + if (string.IsNullOrEmpty(stringToRemove)) + return s; + if (!s.StartsWith(stringToRemove)) + throw new ArgumentException(string.Format("{0} does not start with {1}", s, stringToRemove)); + return s.Substring(stringToRemove.Length); + } + + /// + /// Removes from the end of this string. + /// Throws ArgumentException if this string does not end with . + /// + public static string RemoveEnd(this string s, string stringToRemove) + { + if (s == null) + return null; + if (string.IsNullOrEmpty(stringToRemove)) + return s; + if (!s.EndsWith(stringToRemove)) + throw new ArgumentException(string.Format("{0} does not end with {1}", s, stringToRemove)); + return s.Substring(0, s.Length - stringToRemove.Length); + } + public static string Replace(this string original, string pattern, string replacement, StringComparison comparisonType) { if (original == null)