diff options
author | David Karlaš <david.karlas@microsoft.com> | 2019-04-26 14:37:00 +0300 |
---|---|---|
committer | David Karlaš <david.karlas@microsoft.com> | 2019-04-30 17:57:18 +0300 |
commit | b13f238fc9c8db8cd73a021ff56677ebb607a5d7 (patch) | |
tree | bce1a56367a678d080d0f802fb14fab0d7cff171 /main/src/addins/MonoDevelop.TextEditor | |
parent | 30788a0b1ce672c93186e7e97cf1cd81bafccac1 (diff) |
Initial support for snippets in new editor
CSharpBinding MimeType for C# had wrong casing for ContentType
ExpansionManager implements new interface by converting MD snippets to new editor snippets
Diffstat (limited to 'main/src/addins/MonoDevelop.TextEditor')
2 files changed, 86 insertions, 0 deletions
diff --git a/main/src/addins/MonoDevelop.TextEditor/MonoDevelop.TextEditor.Cocoa/ExpansionManager.cs b/main/src/addins/MonoDevelop.TextEditor/MonoDevelop.TextEditor.Cocoa/ExpansionManager.cs new file mode 100644 index 0000000000..0199381f6c --- /dev/null +++ b/main/src/addins/MonoDevelop.TextEditor/MonoDevelop.TextEditor.Cocoa/ExpansionManager.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.Composition; +using System.IO; +using Microsoft.VisualStudio.Text; +using Microsoft.VisualStudio.Text.Editor; +using Microsoft.VisualStudio.Text.Editor.Expansion; +using Microsoft.VisualStudio.Utilities; +using MonoDevelop.Ide; +using MonoDevelop.Ide.CodeTemplates; + +namespace MonoDevelop.TextEditor.Cocoa +{ + [Export (typeof (IExpansionManager))] + class ExpansionManager : IExpansionManager + { + List<ExpansionTemplate> expansionTemplates; + + public IEnumerable<ExpansionTemplate> EnumerateExpansions (IContentType contentType, bool shortcutOnly, string[] snippetTypes, bool includeNullType, bool includeDuplicates) + { + if (expansionTemplates != null) + return expansionTemplates; + expansionTemplates = new List<ExpansionTemplate> (); + foreach (var codeTemplate in CodeTemplateService.GetCodeTemplates (IdeServices.DesktopService.GetMimeTypeForContentType (contentType))) + expansionTemplates.Add (Convert (codeTemplate)); + return expansionTemplates; + } + + private ExpansionTemplate Convert (CodeTemplate codeTemplate) + { + var codeSnippet = new CodeSnippet (); + codeSnippet.Code = codeTemplate.Code; + codeSnippet.Description = codeTemplate.Description; + codeSnippet.Fields = new List<ExpansionField> (); + foreach (var variable in codeTemplate.Variables) { + var field = new ExpansionField (); + field.Editable = variable.IsEditable; + field.Function = ConvertFunctionName (variable.Function); + field.ToolTip = variable.ToolTip; + field.Default = variable.Default ?? ""; + if ("GetConstructorModifier()" == variable.Function && string.IsNullOrEmpty (field.Default)) + field.Default = "public "; + field.ID = variable.Name; + codeSnippet.Fields.Add (field); + } + codeSnippet.Language = codeTemplate.MimeType; + codeSnippet.Shortcut = codeTemplate.Shortcut; + codeSnippet.Title = codeTemplate.Shortcut; + return new ExpansionTemplate (codeSnippet); + } + + private string ConvertFunctionName (string function) + { + if (function == null) + return function; + if (function == "GetCurrentClassName()") + return "ClassName()"; + if (function.StartsWith ("GetSimpleTypeName", StringComparison.Ordinal)) + return function.Replace ("GetSimpleTypeName", "SimpleTypeName").Replace ("#", ".").Replace ("\"", ""); + return function; + } + + public ExpansionTemplate GetTemplateByName (IExpansionClient expansionClient, IContentType contentType, string name, string filePath, ITextView textView, SnapshotSpan span, bool showDisambiguationUI) + { + foreach (var template in EnumerateExpansions (contentType, false, null, true, true)) + if (template.Snippet.Title == name) + return template; + return null; + } + + public ExpansionTemplate GetTemplateByShortcut (IExpansionClient expansionClient, string shortcut, IContentType contentType, ITextView textView, SnapshotSpan span, bool showDisambiguationUI) + { + foreach (var template in EnumerateExpansions (contentType, false, null, true, true)) + if (template.Snippet.Shortcut == shortcut) + return template; + return null; + } + } +} diff --git a/main/src/addins/MonoDevelop.TextEditor/MonoDevelop.TextEditor/Properties/MonoDevelop.TextEditor.addin.xml b/main/src/addins/MonoDevelop.TextEditor/MonoDevelop.TextEditor/Properties/MonoDevelop.TextEditor.addin.xml index bf2fd3fb95..f2c8641b30 100644 --- a/main/src/addins/MonoDevelop.TextEditor/MonoDevelop.TextEditor/Properties/MonoDevelop.TextEditor.addin.xml +++ b/main/src/addins/MonoDevelop.TextEditor/MonoDevelop.TextEditor/Properties/MonoDevelop.TextEditor.addin.xml @@ -92,6 +92,10 @@ <Map id="MonoDevelop.CSharp.Highlighting.MoveToUsagesCommand.NextUsage" argsType="Microsoft.VisualStudio.Text.Editor.Commanding.Commands.NavigateToNextHighlightedReferenceCommandArgs" /> <Map id="MonoDevelop.CSharp.Highlighting.MoveToUsagesCommand.PrevUsage" argsType="Microsoft.VisualStudio.Text.Editor.Commanding.Commands.NavigateToPreviousHighlightedReferenceCommandArgs" /> + + <Map id="MonoDevelop.Ide.Commands.TextEditorCommands.ShowCodeTemplateWindow" argsType="Microsoft.VisualStudio.Text.Editor.Commanding.Commands.InsertSnippetCommandArgs" /> + <Map id="MonoDevelop.Ide.Commands.TextEditorCommands.ShowCodeSurroundingsWindow" argsType="Microsoft.VisualStudio.Text.Editor.Commanding.Commands.SurroundWithCommandArgs" /> + </Extension> <!-- @@ -188,6 +192,9 @@ <CommandItem id = "MonoDevelop.Ide.Commands.EditCommands.IndentSelection" /> <CommandItem id = "MonoDevelop.Ide.Commands.EditCommands.UnIndentSelection" /> <SeparatorItem id = "Separator3" /> + <CommandItem id = "MonoDevelop.Ide.Commands.TextEditorCommands.ShowCodeSurroundingsWindow" _label = "Surround With..." /> + <CommandItem id = "MonoDevelop.Ide.Commands.TextEditorCommands.ShowCodeTemplateWindow" _label = "Insert Template..." /> + <SeparatorItem id = "Separator4" /> </Extension> <!-- <Extension path = "/MonoDevelop/SourceEditor2/ContextMenu/Scrollbar"> |