diff options
Diffstat (limited to 'main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates')
7 files changed, 249 insertions, 214 deletions
diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/CodeTemplate.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/CodeTemplate.cs index cd4b4c4c64..726c3ae0d3 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/CodeTemplate.cs +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/CodeTemplate.cs @@ -33,9 +33,11 @@ using System.Text; using System.Text.RegularExpressions; using System.Xml; using MonoDevelop.Core; -using Mono.TextEditor; -using Mono.TextEditor.PopupWindow; using MonoDevelop.Ide.CodeFormatting; +using MonoDevelop.Ide.Editor; +using MonoDevelop.Core.Text; +using System.Linq; +using MonoDevelop.Ide.Gui; namespace MonoDevelop.Ide.CodeTemplates { @@ -134,34 +136,34 @@ namespace MonoDevelop.Ide.CodeTemplates return string.Format("[CodeTemplate: Group={0}, Shortcut={1}, CodeTemplateType={2}, MimeType={3}, Description={4}, Code={5}]", Group, Shortcut, CodeTemplateType, MimeType, Description, Code); } - static int FindPrevWordStart (TextEditorData editor, int offset) + static int FindPrevWordStart (TextEditor editor, int offset) { while (--offset >= 0 && !Char.IsWhiteSpace (editor.GetCharAt (offset))) ; return ++offset; } - public static string GetWordBeforeCaret (TextEditorData editor) + public static string GetWordBeforeCaret (TextEditor editor) { - int offset = editor.Caret.Offset; + int offset = editor.CaretOffset; int start = FindPrevWordStart (editor, offset); return editor.GetTextBetween (start, offset); } - static int DeleteWordBeforeCaret (TextEditorData editor) + static int DeleteWordBeforeCaret (TextEditor editor) { - int offset = editor.Caret.Offset; + int offset = editor.CaretOffset; int start = FindPrevWordStart (editor, offset); - editor.Remove (start, offset - start); + editor.RemoveText (start, offset - start); return start; } - static Regex variableRegEx = new Regex ("\\$([^$]*)\\$", RegexOptions.Compiled); + static System.Text.RegularExpressions.Regex variableRegEx = new System.Text.RegularExpressions.Regex ("\\$([^$]*)\\$", RegexOptions.Compiled); public List<string> ParseVariables (string code) { var result = new List<string> (); - foreach (Match match in variableRegEx.Matches (code)) { + foreach (System.Text.RegularExpressions.Match match in variableRegEx.Matches (code)) { string name = match.Groups[1].Value; if (name == "end" || name == "selected" || string.IsNullOrEmpty (name) || name.Trim ().Length == 0) continue; @@ -211,9 +213,9 @@ namespace MonoDevelop.Ide.CodeTemplates var result = new TemplateResult (); var sb = new StringBuilder (); int lastOffset = 0; - string code = context.Document.Editor.FormatString (context.InsertPosition, context.TemplateCode); + string code = context.Editor.FormatString (context.InsertPosition, context.TemplateCode); result.TextLinks = new List<TextLink> (); - foreach (Match match in variableRegEx.Matches (code)) { + foreach (System.Text.RegularExpressions.Match match in variableRegEx.Matches (code)) { string name = match.Groups [1].Value; sb.Append (code.Substring (lastOffset, match.Index - lastOffset)); lastOffset = match.Index + match.Length; @@ -230,7 +232,7 @@ namespace MonoDevelop.Ide.CodeTemplates } if (!variableDecarations.ContainsKey (name)) continue; - TextLink link = result.TextLinks.Find (l => l.Name == name); + var link = result.TextLinks.Find (l => l.Name == name); bool isNew = link == null; if (isNew) { link = new TextLink (name); @@ -271,13 +273,13 @@ namespace MonoDevelop.Ide.CodeTemplates sb.Append (code.Substring (lastOffset, code.Length - lastOffset)); // format & indent template code - var data = new TextEditorData (); + var data = TextEditorFactory.CreateNewDocument (); data.Text = sb.ToString (); - data.Document.TextReplaced += delegate(object sender, DocumentChangeEventArgs e) { - int delta = e.ChangeDelta; + data.TextChanged += delegate(object sender, MonoDevelop.Core.Text.TextChangeEventArgs e) { + int delta = e.InsertionLength - e.RemovalLength; foreach (var link in result.TextLinks) { - link.Links = new List<TextSegment> (link.Links.AdjustSegments (e)); + link.Links = link.Links.AdjustSegments (e).ToList (); } if (result.CaretEndOffset > e.Offset) result.CaretEndOffset += delta; @@ -285,7 +287,6 @@ namespace MonoDevelop.Ide.CodeTemplates IndentCode (data, context.LineIndent); result.Code = data.Text; - data.Dispose (); return result; } @@ -319,12 +320,12 @@ namespace MonoDevelop.Ide.CodeTemplates return result.ToString (); } - static void IndentCode (TextEditorData data, string lineIndent) + static void IndentCode (ITextDocument data, string lineIndent) { for (int i = 1; i < data.LineCount; i++) { var line = data.GetLine (i + 1); if (line.Length > 0) - data.Insert (line.Offset, lineIndent); + data.InsertText (line.Offset, lineIndent); } } @@ -345,10 +346,10 @@ namespace MonoDevelop.Ide.CodeTemplates string RemoveIndent (string text, string indent) { - var doc = new TextDocument (); + var doc = TextEditorFactory.CreateNewDocument (); doc.Text = text; var result = new StringBuilder (); - foreach (DocumentLine line in doc.Lines) { + foreach (var line in doc.GetLines ()) { string curLineIndent = line.GetIndentation (doc); int offset = Math.Min (curLineIndent.Length, indent.Length); result.Append (doc.GetTextBetween (line.Offset + offset, line.EndOffsetIncludingDelimiter)); @@ -358,58 +359,64 @@ namespace MonoDevelop.Ide.CodeTemplates string Reindent (string text, string indent) { - var doc = new TextDocument (); + var doc = TextEditorFactory.CreateNewDocument (); doc.Text = text; var result = new StringBuilder (); - foreach (DocumentLine line in doc.Lines) { + foreach (var line in doc.GetLines ()) { if (result.Length > 0) result.Append (indent); result.Append (doc.GetTextAt (line.SegmentIncludingDelimiter)); } return result.ToString (); } - + public void Insert (MonoDevelop.Ide.Gui.Document document) { - var handler = document.GetContent<ICodeTemplateHandler> (); + Insert (document.Editor, document); + } + + public void Insert (TextEditor editor, DocumentContext context) + { + var handler = context.GetContent<ICodeTemplateHandler> (); if (handler != null) { - handler.InsertTemplate (this, document); + handler.InsertTemplate (this, editor, context); } else { - InsertTemplateContents (document); + InsertTemplateContents (editor, context); } } /// <summary> /// Don't use this unless you're implementing ICodeTemplateWidget. Use Insert instead. /// </summary> - public TemplateResult InsertTemplateContents (MonoDevelop.Ide.Gui.Document document) + public TemplateResult InsertTemplateContents (TextEditor editor, DocumentContext context) { - Mono.TextEditor.TextEditorData data = document.Editor; + var data = editor; - int offset = data.Caret.Offset; + int offset = data.CaretOffset; // string leadingWhiteSpace = GetLeadingWhiteSpace (editor, editor.CursorLine); - var context = new TemplateContext { + var templateCtx = new TemplateContext { Template = this, - Document = document, - ParsedDocument = document.ParsedDocument != null ? document.ParsedDocument.ParsedFile : null, - InsertPosition = data.Caret.Location, - LineIndent = data.Document.GetLineIndent (data.Caret.Line), + DocumentContext = context, + Editor = editor, + //ParsedDocument = context.ParsedDocument != null ? context.ParsedDocument.ParsedFile : null, + InsertPosition = data.CaretLocation, + LineIndent = data.GetLineIndent (data.CaretLocation.Line), TemplateCode = Code }; if (data.IsSomethingSelected) { int start = data.SelectionRange.Offset; - while (Char.IsWhiteSpace (data.Document.GetCharAt (start))) { + while (Char.IsWhiteSpace (data.GetCharAt (start))) { start++; } int end = data.SelectionRange.EndOffset; - while (Char.IsWhiteSpace (data.Document.GetCharAt (end - 1))) { + while (Char.IsWhiteSpace (data.GetCharAt (end - 1))) { end--; } - context.LineIndent = data.Document.GetLineIndent (data.Document.OffsetToLineNumber (start)); - context.SelectedText = RemoveIndent (data.Document.GetTextBetween (start, end), context.LineIndent); - data.Remove (start, end - start); + templateCtx.LineIndent = data.GetLineIndent (data.OffsetToLineNumber (start)); + templateCtx.SelectedText = RemoveIndent (data.GetTextBetween (start, end), templateCtx.LineIndent); + data.RemoveText (start, end - start); offset = start; } else { string word = GetWordBeforeCaret (data).Trim (); @@ -417,9 +424,9 @@ namespace MonoDevelop.Ide.CodeTemplates offset = DeleteWordBeforeCaret (data); } - TemplateResult template = FillVariables (context); + TemplateResult template = FillVariables (templateCtx); template.InsertPosition = offset; - document.Editor.Insert (offset, template.Code); + editor.InsertText (offset, template.Code); int newoffset; if (template.CaretEndOffset >= 0) { @@ -428,13 +435,13 @@ namespace MonoDevelop.Ide.CodeTemplates newoffset = offset + template.Code.Length; } - document.Editor.Caret.Location = document.Editor.OffsetToLocation (newoffset) ; + editor.CaretLocation = editor.OffsetToLocation (newoffset) ; var prettyPrinter = CodeFormatterService.GetFormatter (data.MimeType); if (prettyPrinter != null) { int endOffset = template.InsertPosition + template.Code.Length; var oldVersion = data.Version; - prettyPrinter.OnTheFlyFormat (document, template.InsertPosition, endOffset); + prettyPrinter.OnTheFlyFormat (editor, context, template.InsertPosition, endOffset); foreach (var textLink in template.TextLinks) { for (int i = 0; i < textLink.Links.Count; i++) { var segment = textLink.Links [i]; @@ -446,6 +453,12 @@ namespace MonoDevelop.Ide.CodeTemplates return template; } + public TemplateResult InsertTemplateContents (Document document) + { + if (document == null) + throw new ArgumentNullException ("document"); + return InsertTemplateContents (document.Editor, document); + } #region I/O public const string Node = "CodeTemplate"; const string HeaderNode = "Header"; diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/CodeTemplateCompletionData.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/CodeTemplateCompletionData.cs index 3f19b81ce7..6edaa60d6e 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/CodeTemplateCompletionData.cs +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/CodeTemplateCompletionData.cs @@ -28,20 +28,22 @@ using System; using MonoDevelop.Ide.Gui; using MonoDevelop.Ide.CodeCompletion; using MonoDevelop.Core; +using MonoDevelop.Ide.Editor.Extension; +using MonoDevelop.Ide.Editor; namespace MonoDevelop.Ide.CodeTemplates { public interface ICodeTemplateHandler { - void InsertTemplate (CodeTemplate template, Document document); + void InsertTemplate (CodeTemplate template, TextEditor editor, DocumentContext context); } public class CodeTemplateCompletionData : CompletionData { - Document doc; - CodeTemplate template; + readonly TextEditorExtension doc; + readonly CodeTemplate template; - public CodeTemplateCompletionData (Document doc, CodeTemplate template) + public CodeTemplateCompletionData (TextEditorExtension doc, CodeTemplate template) { this.doc = doc; this.template = template; @@ -51,9 +53,9 @@ namespace MonoDevelop.Ide.CodeTemplates this.Description = template.Shortcut + Environment.NewLine + GettextCatalog.GetString (template.Description); } - public override void InsertCompletionText (CompletionListWindow window, ref KeyActions ka, Gdk.Key closeChar, char keyChar, Gdk.ModifierType modifier) + public override void InsertCompletionText (CompletionListWindow window, ref KeyActions ka, KeyDescriptor descriptor) { - template.Insert (doc); + template.Insert (doc.Editor, doc.DocumentContext); } } } diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/CodeTemplateListDataProvider.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/CodeTemplateListDataProvider.cs index 1487ce6806..30a94e91ef 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/CodeTemplateListDataProvider.cs +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/CodeTemplateListDataProvider.cs @@ -24,9 +24,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; using System.Collections.Generic; -using Mono.TextEditor.PopupWindow; namespace MonoDevelop.Ide.CodeTemplates { diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/CodeTemplatePanel.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/CodeTemplatePanel.cs index f259dfdf67..a454de6047 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/CodeTemplatePanel.cs +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/CodeTemplatePanel.cs @@ -31,6 +31,7 @@ using Gtk; using MonoDevelop.Core; using MonoDevelop.Ide.Gui.Dialogs; using MonoDevelop.Components; +using MonoDevelop.Ide.Editor; namespace MonoDevelop.Ide.CodeTemplates { @@ -41,14 +42,15 @@ namespace MonoDevelop.Ide.CodeTemplates Gtk.TreeStore templateStore; CellRendererText templateCellRenderer; CellRendererImage pixbufCellRenderer; - Mono.TextEditor.TextEditor textEditor = new Mono.TextEditor.TextEditor (); - Mono.TextEditor.TextEditorOptions options; + TextEditor textEditor = TextEditorFactory.CreateNewEditor (); + ITextEditorOptions options; public CodeTemplatePanelWidget (OptionsDialog parent) { this.Build(); - scrolledwindow1.Add (textEditor); - textEditor.ShowAll (); + Gtk.Widget control = textEditor; + scrolledwindow1.Add (control); + control.ShowAll (); templateStore = new TreeStore (typeof (CodeTemplate), typeof (string), typeof (string)); @@ -73,13 +75,9 @@ namespace MonoDevelop.Ide.CodeTemplates treeviewCodeTemplates.ExpandAll (); treeviewCodeTemplates.Selection.Changed += HandleChanged; - - options = new MonoDevelop.Ide.Gui.CommonTextEditorOptions (); - options.ShowLineNumberMargin = false; - options.ShowFoldMargin = false; - options.ShowIconMargin = false; - textEditor.Options = options; - textEditor.Document.ReadOnly = true; + + textEditor.Options = DefaultSourceEditorOptions.PlainEditor; + textEditor.IsReadOnly = true; this.buttonAdd.Clicked += ButtonAddClicked; this.buttonEdit.Clicked += ButtonEditClicked; this.buttonRemove.Clicked += ButtonRemoveClicked; @@ -188,10 +186,10 @@ namespace MonoDevelop.Ide.CodeTemplates CodeTemplate template = templateStore.GetValue (iter, 0) as CodeTemplate; if (template != null) { textEditor.ClearSelection (); - textEditor.Document.MimeType = template.MimeType; - textEditor.Document.Text = template.Code; + textEditor.MimeType = template.MimeType; + textEditor.Text = template.Code; } else { - textEditor.Document.Text = ""; + textEditor.Text = ""; } } } diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/EditTemplateDialog.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/EditTemplateDialog.cs index f81346e749..eedfa7e058 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/EditTemplateDialog.cs +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/EditTemplateDialog.cs @@ -32,6 +32,8 @@ using Gtk; using MonoDevelop.Core; using Gdk; +using MonoDevelop.Ide.Editor; +using MonoDevelop.Core.Text; namespace MonoDevelop.Ide.CodeTemplates @@ -41,8 +43,8 @@ namespace MonoDevelop.Ide.CodeTemplates public partial class EditTemplateDialog : Gtk.Dialog { CodeTemplate template; - Mono.TextEditor.TextEditor textEditor = new Mono.TextEditor.TextEditor (); - Mono.TextEditor.TextEditorOptions options; + TextEditor textEditor = TextEditorFactory.CreateNewEditor (); + ITextEditorOptions options; ListStore variablesListStore; List<CodeTemplateVariable> variables = new List<CodeTemplateVariable> (); @@ -59,24 +61,20 @@ namespace MonoDevelop.Ide.CodeTemplates this.comboboxentryGroups.Entry.Text = template.Group ?? ""; this.comboboxentryMime.Entry.Text = template.MimeType ?? ""; this.entryDescription.Text = template.Description ?? ""; - this.textEditor.Document.MimeType = template.MimeType; - this.textEditor.Document.Text = template.Code; + this.textEditor.MimeType = template.MimeType; + this.textEditor.Text = template.Code; checkbuttonExpansion.Active = (template.CodeTemplateType & CodeTemplateType.Expansion) == CodeTemplateType.Expansion; checkbuttonSurroundWith.Active = (template.CodeTemplateType & CodeTemplateType.SurroundsWith) == CodeTemplateType.SurroundsWith; - scrolledwindow1.Child = textEditor; - textEditor.ShowAll (); - textEditor.Caret.PositionChanged += CaretPositionChanged; - options = new Mono.TextEditor.TextEditorOptions (); - options.ShowLineNumberMargin = false; - options.ShowFoldMargin = false; - options.ShowIconMargin = false; - options.ColorScheme = IdeApp.Preferences.ColorScheme; - textEditor.Options = options; - - HashSet<string> mimeTypes = new HashSet<string> (); - HashSet<string> groups = new HashSet<string> (); + Gtk.Widget control = textEditor; + scrolledwindow1.Child = control; + control.ShowAll (); + textEditor.CaretPositionChanged += CaretPositionChanged; + textEditor.Options = DefaultSourceEditorOptions.PlainEditor; + + var mimeTypes = new HashSet<string> (); + var groups = new HashSet<string> (); foreach (CodeTemplate ct in CodeTemplateService.Templates) { mimeTypes.Add (ct.MimeType); groups.Add (ct.Group); @@ -89,7 +87,7 @@ namespace MonoDevelop.Ide.CodeTemplates foreach (string group in groups) { comboboxentryGroups.AppendText (group); } - textEditor.Document.TextReplaced += DocumentTextReplaced; + textEditor.TextChanged += DocumentTextReplaced; this.buttonOk.Clicked += ButtonOkClicked; checkbuttonWhiteSpaces.Hide (); @@ -140,7 +138,7 @@ namespace MonoDevelop.Ide.CodeTemplates template.Group = this.comboboxentryGroups.Entry.Text; template.MimeType = this.comboboxentryMime.Entry.Text; template.Description = this.entryDescription.Text; - template.Code = this.textEditor.Document.Text; + template.Code = this.textEditor.Text; variables.ForEach (v => template.AddVariable (v)); template.CodeTemplateType = CodeTemplateType.Unknown; if (checkbuttonExpansion.Active) @@ -149,9 +147,9 @@ namespace MonoDevelop.Ide.CodeTemplates template.CodeTemplateType |= CodeTemplateType.SurroundsWith; } - void DocumentTextReplaced (object sender, Mono.TextEditor.DocumentChangeEventArgs e) + void DocumentTextReplaced (object sender, TextChangeEventArgs e) { - List<string> vars = template.ParseVariables (textEditor.Document.Text); + List<string> vars = template.ParseVariables (textEditor.Text); foreach (string var in vars) { if (!variables.Any (v => v.Name == var) && !template.Variables.Any (v => v.Name == var)) { variables.Add (new CodeTemplateVariable (var) { @@ -170,13 +168,13 @@ namespace MonoDevelop.Ide.CodeTemplates } - void CaretPositionChanged (object sender, Mono.TextEditor.DocumentLocationEventArgs e) + void CaretPositionChanged (object sender, EventArgs e) { comboboxVariables.Active = -1; - int offset = textEditor.Caret.Offset; + int offset = textEditor.CaretOffset; int start = offset; - while (start >= 0 && start < textEditor.Document.TextLength) { // caret offset may be behind the text - char ch = textEditor.Document.GetCharAt (start); + while (start >= 0 && start < textEditor.Length) { // caret offset may be behind the text + char ch = textEditor.GetCharAt (start); if (ch == '$') break; if (!char.IsLetterOrDigit (ch) && ch != '_') @@ -185,16 +183,16 @@ namespace MonoDevelop.Ide.CodeTemplates } int end = offset; - while (end < textEditor.Document.TextLength) { - char ch = textEditor.Document.GetCharAt (end); + while (end < textEditor.Length) { + char ch = textEditor.GetCharAt (end); if (ch == '$') break; if (!char.IsLetterOrDigit (ch) && ch != '_') return; end++; } - if (start >= 0 && end < textEditor.Document.TextLength) { - string varName = textEditor.Document.GetTextBetween (start, end).Trim ('$'); + if (start >= 0 && end < textEditor.Length) { + string varName = textEditor.GetTextBetween (start, end).Trim ('$'); TreeIter iter; if (variablesListStore.GetIterFirst (out iter)) { int i = -1; diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/ExpansionObject.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/ExpansionObject.cs index 721f645107..9e3fba71fe 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/ExpansionObject.cs +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/ExpansionObject.cs @@ -26,17 +26,19 @@ using System; using System.Collections.Generic; -using System.Text; using System.Text.RegularExpressions; using MonoDevelop.Ide.Gui.Content; -using Mono.TextEditor.PopupWindow; -using Mono.TextEditor; -using ICSharpCode.NRefactory.TypeSystem; using MonoDevelop.Ide.TypeSystem; -using ICSharpCode.NRefactory.Completion; using MonoDevelop.Ide.CodeCompletion; -using ICSharpCode.NRefactory.TypeSystem.Implementation; +using MonoDevelop.Ide.Tasks; +using Microsoft.CodeAnalysis; +using System.Threading.Tasks; +using System.Linq; +using ICSharpCode.NRefactory6.CSharp; +using ICSharpCode.NRefactory6.CSharp.Completion; +using MonoDevelop.Ide.Editor; +using MonoDevelop.Ide.Editor.Extension; namespace MonoDevelop.Ide.CodeTemplates { @@ -47,17 +49,15 @@ namespace MonoDevelop.Ide.CodeTemplates set; } - public ICompilation Compilation { + public SemanticModel Compilation { get { - return Document.Compilation; + var analysisDocument = DocumentContext.ParsedDocument; + if (analysisDocument == null) + return null; + return analysisDocument.GetAst<SemanticModel> (); } } - - public IUnresolvedFile ParsedDocument { - get; - set; - } - + public DocumentLocation InsertPosition { get; set; @@ -78,7 +78,12 @@ namespace MonoDevelop.Ide.CodeTemplates set; } - public MonoDevelop.Ide.Gui.Document Document { + public DocumentContext DocumentContext { + get; + set; + } + + public TextEditor Editor { get; set; } @@ -93,36 +98,28 @@ namespace MonoDevelop.Ide.CodeTemplates public string GetCurrentClassName () { - if (CurrentContext.ParsedDocument == null) - return null; - IUnresolvedTypeDefinition type = null; - var provider = CurrentContext.Document.GetContent<ITextEditorMemberPositionProvider>(); - if (provider == null) { - type = CurrentContext.ParsedDocument.GetInnermostTypeDefinition (CurrentContext.InsertPosition.Line, CurrentContext.InsertPosition.Column); - } else { - type = provider.GetTypeAt (CurrentContext.Document.Editor.LocationToOffset (CurrentContext.InsertPosition)); - } - - if (type == null) + var compilation = CurrentContext.Compilation; + if (compilation == null) return null; - return type.Name; + var enclosingSymbol = compilation.GetEnclosingSymbol (CurrentContext.Editor.CaretOffset); + + if (!(enclosingSymbol is ITypeSymbol)) + enclosingSymbol = enclosingSymbol.ContainingType; + + return enclosingSymbol != null ? enclosingSymbol.Name : null; } public string GetConstructorModifier () { - if (CurrentContext.ParsedDocument == null) + var compilation = CurrentContext.Compilation; + if (compilation == null) return null; - IUnresolvedTypeDefinition type = null; - var provider = CurrentContext.Document.GetContent<ITextEditorMemberPositionProvider>(); - if (provider == null) { - type = CurrentContext.ParsedDocument.GetInnermostTypeDefinition (CurrentContext.InsertPosition.Line, CurrentContext.InsertPosition.Column); - } else { - type = provider.GetTypeAt (CurrentContext.Document.Editor.LocationToOffset (CurrentContext.InsertPosition)); - } - - if (type == null) - return ""; - return type.IsStatic ? "static " : "public "; + var enclosingSymbol = compilation.GetEnclosingSymbol (CurrentContext.Editor.CaretOffset); + + if (!(enclosingSymbol is ITypeSymbol)) + enclosingSymbol = enclosingSymbol.ContainingType; + + return enclosingSymbol != null && enclosingSymbol.IsStatic ? "static " : "public "; } public string GetLengthProperty (Func<string, string> callback, string varName) @@ -132,29 +129,27 @@ namespace MonoDevelop.Ide.CodeTemplates string var = callback (varName); - ITextEditorResolver textEditorResolver = CurrentContext.Document.GetContent <ITextEditorResolver> (); + ITextEditorResolver textEditorResolver = CurrentContext.DocumentContext.GetContent <ITextEditorResolver> (); if (textEditorResolver != null) { - var result = textEditorResolver.GetLanguageItem (CurrentContext.Document.Editor.Document.LocationToOffset (CurrentContext.InsertPosition), var); - if (result.Type.IsReferenceType.HasValue && !result.Type.IsReferenceType.Value) - return "Length"; + var result = textEditorResolver.GetLanguageItem (CurrentContext.Editor.LocationToOffset (CurrentContext.InsertPosition), var); + if (result != null) { + var returnType = result.GetReturnType (); + if (returnType != null && !returnType.IsReferenceType) + return "Length"; + } } return "Count"; } - IType GetElementType (IType result) + ITypeSymbol GetElementType (ITypeSymbol type) { - foreach (var baseType in result.GetAllBaseTypes ()) { - var baseTypeDef = baseType.GetDefinition(); - if (baseTypeDef != null && baseTypeDef.Name == "IEnumerable") { - if (baseTypeDef.Namespace == "System.Collections.Generic" && baseTypeDef.TypeParameterCount == 1) { - if (baseType.TypeArguments.Count > 0) - return baseType.TypeArguments[0]; - } else if (baseTypeDef.Namespace == "System.Collections" && baseTypeDef.TypeParameterCount == 0) { - return CurrentContext.Compilation.FindType (KnownTypeCode.Object); - } + foreach (var baseType in type.AllInterfaces) { + if (baseType != null && baseType.Name == "IEnumerable") { + if (baseType.TypeArguments.Length > 0) + return baseType.TypeArguments[0]; } } - return new UnknownType ("", "", 0); + return type; } @@ -162,70 +157,59 @@ namespace MonoDevelop.Ide.CodeTemplates { if (callback == null) return "var"; - + var compilation = CurrentContext.Compilation; + if (compilation == null) + return null; + string var = callback (varName); - ITextEditorResolver textEditorResolver = CurrentContext.Document.GetContent <ITextEditorResolver> (); - if (textEditorResolver != null) { - var result = textEditorResolver.GetLanguageItem (CurrentContext.Document.Editor.Caret.Offset, var); - if (result != null) { - var componentType = GetElementType (result.Type); - if (componentType.Kind != TypeKind.Unknown) { - var generator = CodeGenerator.CreateGenerator (CurrentContext.Document); - if (generator != null) - return generator.GetShortTypeString (CurrentContext.Document, componentType); - } - } - } - + + var offset = CurrentContext.Editor.CaretOffset; + var sym = compilation.LookupSymbols (offset).First (s => s.Name == var); + if (sym == null) + return "var"; + var rt = sym.GetReturnType (); + if (rt != null) + return rt.ToMinimalDisplayString (compilation, offset); return "var"; } - MonoDevelop.Ide.CodeCompletion.ICompletionDataList list; + + ICompletionDataList list; public IListDataProvider<string> GetCollections () { var result = new List<CodeTemplateVariableValue> (); - var ext = CurrentContext.Document.GetContent <CompletionTextEditorExtension> (); + var ext = CurrentContext.DocumentContext.GetContent <CompletionTextEditorExtension> (); if (ext != null) { if (list == null) list = ext.CodeCompletionCommand ( - CurrentContext.Document.GetContent <MonoDevelop.Ide.CodeCompletion.ICompletionWidget> ().CurrentCodeCompletionContext); + CurrentContext.DocumentContext.GetContent <MonoDevelop.Ide.CodeCompletion.ICompletionWidget> ().CurrentCodeCompletionContext); - foreach (object o in list) { - var data = o as IEntityCompletionData; - if (data == null) - continue; - - if (data.Entity is IMember) { - var m = data.Entity as IMember; - if (GetElementType (m.ReturnType).Kind != TypeKind.Unknown) { - if (m is IMethod) { - if (((IMethod)m).Parameters.Count == 0) - result.Add (new CodeTemplateVariableValue (m.Name + " ()", ((CompletionData)data).Icon)); - continue; - } - - result.Add (new CodeTemplateVariableValue (m.Name, ((CompletionData)data).Icon)); + foreach (var data in list.OfType<ISymbolCompletionData> ()) { + if (GetElementType (data.Symbol.GetReturnType ()).TypeKind != TypeKind.Error) { + var method = data as IMethodSymbol; + if (method != null) { + if (method.Parameters.Length == 0) + result.Add (new CodeTemplateVariableValue (data.Symbol.Name + " ()", ((CompletionData)data).Icon)); + continue; } + + result.Add (new CodeTemplateVariableValue (data.Symbol.Name, ((CompletionData)data).Icon)); } } - foreach (object o in list) { - var data = o as IEntityCompletionData; - if (data == null) - continue; - if (data.Entity is IParameter) { - var m = data.Entity as IParameter; - if (GetElementType (m.Type).Kind != TypeKind.Unknown) + foreach (var data in list.OfType<ISymbolCompletionData> ()) { + var m = data.Symbol as IParameterSymbol; + if (m != null) { + if (GetElementType (m.Type).TypeKind != TypeKind.Error) result.Add (new CodeTemplateVariableValue (m.Name, ((CompletionData)data).Icon)); } } - foreach (object o in list) { - var data = o as IVariableCompletionData; - if (data == null) + foreach (var sym in list.OfType<ISymbolCompletionData> ()) { + var m = sym.Symbol as ILocalSymbol; + if (m == null) continue; - var m = data.Variable; - if (GetElementType (m.Type).Kind != TypeKind.Unknown) - result.Add (new CodeTemplateVariableValue (m.Name, ((CompletionData)data).Icon)); + if (GetElementType (m.Type).TypeKind != TypeKind.Error) + result.Add (new CodeTemplateVariableValue (m.Name, ((CompletionData)m).Icon)); } } return new CodeTemplateListDataProvider (result); @@ -233,7 +217,8 @@ namespace MonoDevelop.Ide.CodeTemplates public string GetSimpleTypeName (string fullTypeName) { - if (CurrentContext.ParsedDocument == null) + var compilation = CurrentContext.Compilation; + if (compilation == null) return fullTypeName.Replace ("#", "."); string ns = ""; string name = ""; @@ -249,31 +234,21 @@ namespace MonoDevelop.Ide.CodeTemplates idx = name.IndexOf ('.'); if (idx >= 0) { - member = name.Substring (idx); + member = name.Substring (idx + 1); name = name.Substring (0, idx); } - var type = new GetClassTypeReference (ns, name, 0).Resolve (new SimpleTypeResolveContext (CurrentContext.Document.Compilation.MainAssembly)); - bool stripAttribute = false; - if (type == null || type.Kind == TypeKind.Unknown) { - type = new GetClassTypeReference (ns, name + "Attribute", 0).Resolve ( - new SimpleTypeResolveContext (CurrentContext.Document.Compilation.MainAssembly) - ); - stripAttribute = true; - } - if (type == null || type.Kind == TypeKind.Unknown) - return fullTypeName.Replace ("#", "."); - var generator = CodeGenerator.CreateGenerator (CurrentContext.Document); - if (generator != null) { - var result = generator.GetShortTypeString (CurrentContext.Document, type) + member; - if (stripAttribute && result.EndsWith ("Attribute", StringComparison.Ordinal)) - result = result.Substring (0, result.Length - "Attribute".Length); - return result; + var metadataName = string.IsNullOrEmpty (ns) ? name : ns + "." + name; + var type = compilation.Compilation.GetTypeByMetadataName (metadataName); + if (type != null) { + var minimalName = type.ToMinimalDisplayString (compilation, CurrentContext.Editor.CaretOffset); + return string.IsNullOrEmpty (member) ? minimalName : minimalName + "." + member; } return fullTypeName.Replace ("#", "."); } - static Regex functionRegEx = new Regex ("([^(]*)\\(([^(]*)\\)", RegexOptions.Compiled); + + static System.Text.RegularExpressions.Regex functionRegEx = new System.Text.RegularExpressions.Regex ("([^(]*)\\(([^(]*)\\)", RegexOptions.Compiled); // We should use reflection here (but for 5 functions it doesn't hurt) !!! - Mike @@ -294,7 +269,7 @@ namespace MonoDevelop.Ide.CodeTemplates public virtual IListDataProvider<string> RunFunction (TemplateContext context, Func<string, string> callback, string function) { this.CurrentContext = context; - Match match = functionRegEx.Match (function); + var match = functionRegEx.Match (function); if (!match.Success) return null; string name = match.Groups[1].Value; diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/IListDataProvider.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/IListDataProvider.cs new file mode 100644 index 0000000000..b1d02b8310 --- /dev/null +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeTemplates/IListDataProvider.cs @@ -0,0 +1,51 @@ +// +// IListDataProvider.cs +// +// Author: +// Mike Krüger <mkrueger@novell.com> +// +// Copyright (c) 2009 Novell, Inc (http://www.novell.com) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using System; +using System.Collections.Generic; + +namespace MonoDevelop.Ide.CodeTemplates +{ + public interface IListDataProvider<T> + { + int Count { + get; + } + + T this[int index] { + get; + } + + Xwt.Drawing.Image GetIcon (int index); + string GetText (int index); + } + + public interface IMarkupListDataProvider<T> : IListDataProvider<T> + { + bool HasMarkup (int index); + string GetMarkup (int index); + } +}
\ No newline at end of file |