Browse Source
git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@5069 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61shortcuts
48 changed files with 1155 additions and 998 deletions
@ -0,0 +1,20 @@ |
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 10.00 |
||||||
|
# Visual Studio 2008 |
||||||
|
# SharpDevelop 4.0.0.5064 |
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AvalonEdit.Sample", "AvalonEdit.Sample.csproj", "{13A5B497-BA12-45AE-9033-22620C3153FB}" |
||||||
|
EndProject |
||||||
|
Global |
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution |
||||||
|
Debug|x86 = Debug|x86 |
||||||
|
Release|x86 = Release|x86 |
||||||
|
Debug|x86 = Debug|x86 |
||||||
|
Release|x86 = Release|x86 |
||||||
|
EndGlobalSection |
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution |
||||||
|
{13A5B497-BA12-45AE-9033-22620C3153FB}.Debug|x86.Build.0 = Debug|x86 |
||||||
|
{13A5B497-BA12-45AE-9033-22620C3153FB}.Debug|x86.ActiveCfg = Debug|x86 |
||||||
|
{13A5B497-BA12-45AE-9033-22620C3153FB}.Release|x86.Build.0 = Release|x86 |
||||||
|
{13A5B497-BA12-45AE-9033-22620C3153FB}.Release|x86.ActiveCfg = Release|x86 |
||||||
|
EndGlobalSection |
||||||
|
EndGlobal |
@ -0,0 +1,192 @@ |
|||||||
|
// <file>
|
||||||
|
// <copyright see="prj:///doc/copyright.txt"/>
|
||||||
|
// <license see="prj:///doc/license.txt"/>
|
||||||
|
// <owner name="Daniel Grunwald"/>
|
||||||
|
// <version>$Revision$</version>
|
||||||
|
// </file>
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.ComponentModel; |
||||||
|
using System.Text.RegularExpressions; |
||||||
|
|
||||||
|
using ICSharpCode.AvalonEdit.Snippets; |
||||||
|
using ICSharpCode.SharpDevelop; |
||||||
|
using ICSharpCode.SharpDevelop.Dom; |
||||||
|
using ICSharpCode.SharpDevelop.Dom.Refactoring; |
||||||
|
using ICSharpCode.SharpDevelop.Editor; |
||||||
|
using ICSharpCode.SharpDevelop.Editor.CodeCompletion; |
||||||
|
|
||||||
|
namespace ICSharpCode.AvalonEdit.AddIn.Snippets |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// A code snippet.
|
||||||
|
/// </summary>
|
||||||
|
public class CodeSnippet : INotifyPropertyChanged |
||||||
|
{ |
||||||
|
string name, description, text; |
||||||
|
|
||||||
|
public string Name { |
||||||
|
get { return name; } |
||||||
|
set { |
||||||
|
if (name != value) { |
||||||
|
name = value; |
||||||
|
OnPropertyChanged("Name"); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public string Text { |
||||||
|
get { return text; } |
||||||
|
set { |
||||||
|
if (text != value) { |
||||||
|
text = value; |
||||||
|
OnPropertyChanged("Text"); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public string Description { |
||||||
|
get { return description; } |
||||||
|
set { |
||||||
|
if (description != value) { |
||||||
|
description = value; |
||||||
|
OnPropertyChanged("Description"); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public event PropertyChangedEventHandler PropertyChanged; |
||||||
|
|
||||||
|
protected virtual void OnPropertyChanged(string propertyName) |
||||||
|
{ |
||||||
|
if (PropertyChanged != null) { |
||||||
|
PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public Snippet CreateAvalonEditSnippet(ITextEditor context) |
||||||
|
{ |
||||||
|
return CreateAvalonEditSnippet(context, this.Text); |
||||||
|
} |
||||||
|
|
||||||
|
public ICompletionItem CreateCompletionItem(ITextEditor context) |
||||||
|
{ |
||||||
|
return new SnippetCompletionItem(context, this); |
||||||
|
} |
||||||
|
|
||||||
|
readonly static Regex pattern = new Regex(@"\$\{([^\}]*)\}", RegexOptions.CultureInvariant); |
||||||
|
|
||||||
|
public static Snippet CreateAvalonEditSnippet(ITextEditor context, string snippetText) |
||||||
|
{ |
||||||
|
if (snippetText == null) |
||||||
|
throw new ArgumentNullException("text"); |
||||||
|
var replaceableElements = new Dictionary<string, SnippetReplaceableTextElement>(StringComparer.OrdinalIgnoreCase); |
||||||
|
foreach (Match m in pattern.Matches(snippetText)) { |
||||||
|
string val = m.Groups[1].Value; |
||||||
|
int equalsSign = val.IndexOf('='); |
||||||
|
if (equalsSign > 0) { |
||||||
|
string name = val.Substring(0, equalsSign); |
||||||
|
replaceableElements[name] = new SnippetReplaceableTextElement(); |
||||||
|
} |
||||||
|
} |
||||||
|
Snippet snippet = new Snippet(); |
||||||
|
int pos = 0; |
||||||
|
foreach (Match m in pattern.Matches(snippetText)) { |
||||||
|
if (pos < m.Index) { |
||||||
|
snippet.Elements.Add(new SnippetTextElement { Text = snippetText.Substring(pos, m.Index - pos) }); |
||||||
|
pos = m.Index; |
||||||
|
} |
||||||
|
snippet.Elements.Add(CreateElementForValue(context, replaceableElements, m.Groups[1].Value)); |
||||||
|
pos = m.Index + m.Length; |
||||||
|
} |
||||||
|
if (pos < snippetText.Length) { |
||||||
|
snippet.Elements.Add(new SnippetTextElement { Text = snippetText.Substring(pos) }); |
||||||
|
} |
||||||
|
return snippet; |
||||||
|
} |
||||||
|
|
||||||
|
readonly static Regex functionPattern = new Regex(@"^([a-zA-Z]+)\(([^\)]*)\)$", RegexOptions.CultureInvariant); |
||||||
|
|
||||||
|
static SnippetElement CreateElementForValue(ITextEditor context, Dictionary<string, SnippetReplaceableTextElement> replaceableElements, string val) |
||||||
|
{ |
||||||
|
SnippetReplaceableTextElement srte; |
||||||
|
int equalsSign = val.IndexOf('='); |
||||||
|
if (equalsSign > 0) { |
||||||
|
string name = val.Substring(0, equalsSign); |
||||||
|
if (replaceableElements.TryGetValue(name, out srte)) { |
||||||
|
if (srte.Text == null) |
||||||
|
srte.Text = val.Substring(equalsSign + 1); |
||||||
|
return srte; |
||||||
|
} |
||||||
|
} |
||||||
|
if ("Selection".Equals(val, StringComparison.OrdinalIgnoreCase)) |
||||||
|
return new SnippetCaretElement(); |
||||||
|
if (replaceableElements.TryGetValue(val, out srte)) |
||||||
|
return new SnippetBoundElement { TargetElement = srte }; |
||||||
|
Match m = functionPattern.Match(val); |
||||||
|
if (m.Success) { |
||||||
|
Func<string, string> f = GetFunction(context, m.Groups[1].Value); |
||||||
|
if (f != null) { |
||||||
|
string innerVal = m.Groups[2].Value; |
||||||
|
if (replaceableElements.TryGetValue(innerVal, out srte)) |
||||||
|
return new FunctionBoundElement { TargetElement = srte, function = f }; |
||||||
|
string result2 = GetValue(context, innerVal); |
||||||
|
if (result2 != null) |
||||||
|
return new SnippetTextElement { Text = f(result2) }; |
||||||
|
else |
||||||
|
return new SnippetTextElement { Text = f(innerVal) }; |
||||||
|
} |
||||||
|
} |
||||||
|
string result = GetValue(context, val); |
||||||
|
if (result != null) |
||||||
|
return new SnippetTextElement { Text = result }; |
||||||
|
else |
||||||
|
return new SnippetReplaceableTextElement { Text = val }; // ${unknown} -> replaceable element
|
||||||
|
} |
||||||
|
|
||||||
|
static string GetValue(ITextEditor editor, string propertyName) |
||||||
|
{ |
||||||
|
if ("ClassName".Equals(propertyName, StringComparison.OrdinalIgnoreCase)) { |
||||||
|
IClass c = GetCurrentClass(editor); |
||||||
|
if (c != null) |
||||||
|
return c.Name; |
||||||
|
} |
||||||
|
return Core.StringParser.GetValue(propertyName); |
||||||
|
} |
||||||
|
|
||||||
|
static IClass GetCurrentClass(ITextEditor editor) |
||||||
|
{ |
||||||
|
var parseInfo = ParserService.GetExistingParseInformation(editor.FileName); |
||||||
|
if (parseInfo != null) { |
||||||
|
return parseInfo.CompilationUnit.GetInnermostClass(editor.Caret.Line, editor.Caret.Column); |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
static Func<string, string> GetFunction(ITextEditor context, string name) |
||||||
|
{ |
||||||
|
if ("toLower".Equals(name, StringComparison.OrdinalIgnoreCase)) |
||||||
|
return s => s.ToLower(); |
||||||
|
if ("toUpper".Equals(name, StringComparison.OrdinalIgnoreCase)) |
||||||
|
return s => s.ToUpper(); |
||||||
|
if ("toFieldName".Equals(name, StringComparison.OrdinalIgnoreCase)) |
||||||
|
return s => context.Language.Properties.CodeGenerator.GetFieldName(s); |
||||||
|
if ("toPropertyName".Equals(name, StringComparison.OrdinalIgnoreCase)) |
||||||
|
return s => context.Language.Properties.CodeGenerator.GetPropertyName(s); |
||||||
|
if ("toParameterName".Equals(name, StringComparison.OrdinalIgnoreCase)) |
||||||
|
return s => context.Language.Properties.CodeGenerator.GetParameterName(s); |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
sealed class FunctionBoundElement : SnippetBoundElement |
||||||
|
{ |
||||||
|
internal Func<string, string> function; |
||||||
|
|
||||||
|
public override string ConvertText(string input) |
||||||
|
{ |
||||||
|
return function(input); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,47 @@ |
|||||||
|
// <file>
|
||||||
|
// <copyright see="prj:///doc/copyright.txt"/>
|
||||||
|
// <license see="prj:///doc/license.txt"/>
|
||||||
|
// <owner name="Daniel Grunwald"/>
|
||||||
|
// <version>$Revision$</version>
|
||||||
|
// </file>
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Collections.ObjectModel; |
||||||
|
using System.ComponentModel; |
||||||
|
|
||||||
|
namespace ICSharpCode.AvalonEdit.AddIn.Snippets |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// A group of snippets (for a specific file extension).
|
||||||
|
/// </summary>
|
||||||
|
public class CodeSnippetGroup : INotifyPropertyChanged |
||||||
|
{ |
||||||
|
string extensions = ""; |
||||||
|
ObservableCollection<CodeSnippet> snippets = new ObservableCollection<CodeSnippet>(); |
||||||
|
|
||||||
|
public ObservableCollection<CodeSnippet> Snippets { |
||||||
|
get { return snippets; } |
||||||
|
} |
||||||
|
|
||||||
|
public string Extensions { |
||||||
|
get { return extensions; } |
||||||
|
set { |
||||||
|
if (value == null) |
||||||
|
throw new ArgumentNullException(); |
||||||
|
if (extensions != value) { |
||||||
|
extensions = value; |
||||||
|
OnPropertyChanged("Extensions"); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public event PropertyChangedEventHandler PropertyChanged; |
||||||
|
|
||||||
|
protected virtual void OnPropertyChanged(string propertyName) |
||||||
|
{ |
||||||
|
if (PropertyChanged != null) { |
||||||
|
PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,94 @@ |
|||||||
|
// <file>
|
||||||
|
// <copyright see="prj:///doc/copyright.txt"/>
|
||||||
|
// <license see="prj:///doc/license.txt"/>
|
||||||
|
// <owner name="Daniel Grunwald"/>
|
||||||
|
// <version>$Revision$</version>
|
||||||
|
// </file>
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Windows.Controls; |
||||||
|
using System.Windows.Documents; |
||||||
|
|
||||||
|
using ICSharpCode.AvalonEdit.Editing; |
||||||
|
using ICSharpCode.AvalonEdit.Snippets; |
||||||
|
using ICSharpCode.Core; |
||||||
|
using ICSharpCode.SharpDevelop; |
||||||
|
using ICSharpCode.SharpDevelop.Editor; |
||||||
|
using ICSharpCode.SharpDevelop.Editor.CodeCompletion; |
||||||
|
|
||||||
|
namespace ICSharpCode.AvalonEdit.AddIn.Snippets |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Code completion item for snippets.
|
||||||
|
/// </summary>
|
||||||
|
public class SnippetCompletionItem : IFancyCompletionItem |
||||||
|
{ |
||||||
|
readonly CodeSnippet codeSnippet; |
||||||
|
readonly ITextEditor textEditor; |
||||||
|
readonly TextArea textArea; |
||||||
|
|
||||||
|
public SnippetCompletionItem(ITextEditor textEditor, CodeSnippet codeSnippet) |
||||||
|
{ |
||||||
|
if (textEditor == null) |
||||||
|
throw new ArgumentNullException("textEditor"); |
||||||
|
if (codeSnippet == null) |
||||||
|
throw new ArgumentNullException("codeSnippet"); |
||||||
|
this.textEditor = textEditor; |
||||||
|
this.textArea = textEditor.GetService(typeof(TextArea)) as TextArea; |
||||||
|
if (textArea == null) |
||||||
|
throw new ArgumentException("textEditor must be an AvalonEdit text editor"); |
||||||
|
this.codeSnippet = codeSnippet; |
||||||
|
} |
||||||
|
|
||||||
|
public string Text { |
||||||
|
get { return codeSnippet.Name; } |
||||||
|
} |
||||||
|
|
||||||
|
public string Description { |
||||||
|
get { |
||||||
|
return codeSnippet.Description + Environment.NewLine + |
||||||
|
ResourceService.GetString("Dialog.Options.CodeTemplate.PressTabToInsertTemplate"); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public ICSharpCode.SharpDevelop.IImage Image { |
||||||
|
get { |
||||||
|
return ClassBrowserIconService.CodeTemplate; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public void Complete(CompletionContext context) |
||||||
|
{ |
||||||
|
if (context.Editor != this.textEditor) |
||||||
|
throw new ArgumentException("wrong editor"); |
||||||
|
context.Editor.Document.Remove(context.StartOffset, context.Length); |
||||||
|
CreateSnippet().Insert(textArea); |
||||||
|
} |
||||||
|
|
||||||
|
Snippet CreateSnippet() |
||||||
|
{ |
||||||
|
return codeSnippet.CreateAvalonEditSnippet(textEditor); |
||||||
|
} |
||||||
|
|
||||||
|
object IFancyCompletionItem.Content { |
||||||
|
get { |
||||||
|
return Text; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
TextBlock fancyDescription; |
||||||
|
|
||||||
|
object IFancyCompletionItem.Description { |
||||||
|
get { |
||||||
|
if (fancyDescription == null) { |
||||||
|
fancyDescription = new TextBlock(); |
||||||
|
fancyDescription.Inlines.Add(new Run(this.Description + Environment.NewLine + Environment.NewLine)); |
||||||
|
Inline r = CreateSnippet().ToTextRun(); |
||||||
|
r.FontFamily = textArea.FontFamily; |
||||||
|
fancyDescription.Inlines.Add(r); |
||||||
|
} |
||||||
|
return fancyDescription; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,153 @@ |
|||||||
|
// <file>
|
||||||
|
// <copyright see="prj:///doc/copyright.txt"/>
|
||||||
|
// <license see="prj:///doc/license.txt"/>
|
||||||
|
// <owner name="Daniel Grunwald"/>
|
||||||
|
// <version>$Revision$</version>
|
||||||
|
// </file>
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Collections.ObjectModel; |
||||||
|
using System.Linq; |
||||||
|
|
||||||
|
using ICSharpCode.Core; |
||||||
|
|
||||||
|
namespace ICSharpCode.AvalonEdit.AddIn.Snippets |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// SnippetManager singleton.
|
||||||
|
/// </summary>
|
||||||
|
public sealed class SnippetManager |
||||||
|
{ |
||||||
|
public static readonly SnippetManager Instance = new SnippetManager(); |
||||||
|
readonly object lockObj = new object(); |
||||||
|
static readonly List<CodeSnippetGroup> defaultSnippets = new List<CodeSnippetGroup> { |
||||||
|
new CodeSnippetGroup { |
||||||
|
Extensions = ".cs", |
||||||
|
Snippets = { |
||||||
|
new CodeSnippet { |
||||||
|
Name = "for", |
||||||
|
Description = "for loop", |
||||||
|
Text = "for (int ${counter=i} = 0; ${counter} < ${end}; ${counter}++) {\n\t${Selection}\n}" |
||||||
|
}, |
||||||
|
new CodeSnippet { |
||||||
|
Name = "foreach", |
||||||
|
Description = "foreach loop", |
||||||
|
Text = "foreach (${var} ${element} in ${collection}) {\n\t${Selection}\n}" |
||||||
|
}, |
||||||
|
new CodeSnippet { |
||||||
|
Name = "if", |
||||||
|
Description = "if statement", |
||||||
|
Text = "if (${condition}) {\n\t${Selection}\n}" |
||||||
|
}, |
||||||
|
new CodeSnippet { |
||||||
|
Name = "ifelse", |
||||||
|
Description = "if-else statement", |
||||||
|
Text = "if (${condition}) {\n\t${Selection}\n} else {\n\t\n}" |
||||||
|
}, |
||||||
|
new CodeSnippet { |
||||||
|
Name = "while", |
||||||
|
Description = "while loop", |
||||||
|
Text = "while (${condition}) {\n\t${Selection}\n}" |
||||||
|
}, |
||||||
|
new CodeSnippet { |
||||||
|
Name = "prop", |
||||||
|
Description = "Property", |
||||||
|
Text = "${type} ${toFieldName(name)};\n\npublic ${type=int} ${name=Property} {\n\tget { return ${toFieldName(name)}; }\n\tset { ${toFieldName(name)} = value; }\n}" |
||||||
|
}, |
||||||
|
new CodeSnippet { |
||||||
|
Name = "propdp", |
||||||
|
Description = "Dependency Property", |
||||||
|
Text = "public static readonly DependencyProperty ${name}Property =" + Environment.NewLine |
||||||
|
+ "\tDependencyProperty.Register(\"${name}\", typeof(${type}), typeof(${ClassName})," + Environment.NewLine |
||||||
|
+ "\t new FrameworkPropertyMetadata());" + Environment.NewLine |
||||||
|
+ "" + Environment.NewLine |
||||||
|
+ "public ${type=int} ${name=Property} {" + Environment.NewLine |
||||||
|
+ "\tget { return (${type})GetValue(${name}Property); }" + Environment.NewLine |
||||||
|
+ "\tset { SetValue(${name}Property, value); }" |
||||||
|
+ Environment.NewLine + "}" |
||||||
|
}, |
||||||
|
new CodeSnippet { |
||||||
|
Name = "ctor", |
||||||
|
Description = "Constructor", |
||||||
|
Text = "public ${ClassName}()\n{\t\n${Selection}\n}" |
||||||
|
}, |
||||||
|
new CodeSnippet { |
||||||
|
Name = "switch", |
||||||
|
Description = "Switch statement", |
||||||
|
Text = "switch (${condition}) {\n\tcase ${firstcase=0}:\n\t\tbreak;\n\tdefault:\n\t\t${Selection}\n\t\tbreak;\n}" |
||||||
|
}, |
||||||
|
new CodeSnippet { |
||||||
|
Name = "try", |
||||||
|
Description = "Try-catch statement", |
||||||
|
Text = "try {\n\t${Selection}\n} catch (Exception) {\n\t\n\tthrow;\n}" |
||||||
|
}, |
||||||
|
new CodeSnippet { |
||||||
|
Name = "trycf", |
||||||
|
Description = "Try-catch-finally statement", |
||||||
|
Text = "try {\n\t${Selection}\n} catch (Exception) {\n\t\n\tthrow;\n} finally {\n\t\n}" |
||||||
|
}, |
||||||
|
new CodeSnippet { |
||||||
|
Name = "tryf", |
||||||
|
Description = "Try-finally statement", |
||||||
|
Text = "try {\n\t${Selection}\n} finally {\n\t\n}" |
||||||
|
}, |
||||||
|
} |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
private SnippetManager() {} |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Loads copies of all code snippet groups.
|
||||||
|
/// </summary>
|
||||||
|
public List<CodeSnippetGroup> LoadGroups() |
||||||
|
{ |
||||||
|
return PropertyService.Get("CodeSnippets", defaultSnippets); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Saves the set of groups.
|
||||||
|
/// </summary>
|
||||||
|
public void SaveGroups(IEnumerable<CodeSnippetGroup> groups) |
||||||
|
{ |
||||||
|
lock (lockObj) { |
||||||
|
activeGroups = null; |
||||||
|
PropertyService.Set("CodeSnippets", groups.ToList()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
ReadOnlyCollection<CodeSnippetGroup> activeGroups; |
||||||
|
|
||||||
|
public ReadOnlyCollection<CodeSnippetGroup> ActiveGroups { |
||||||
|
get { |
||||||
|
lock (lockObj) { |
||||||
|
if (activeGroups == null) |
||||||
|
activeGroups = LoadGroups().AsReadOnly(); |
||||||
|
return activeGroups; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public CodeSnippetGroup FindGroup(string extension) |
||||||
|
{ |
||||||
|
foreach (CodeSnippetGroup g in ActiveGroups) { |
||||||
|
string[] extensions = g.Extensions.Split(';'); |
||||||
|
foreach (string gext in extensions) { |
||||||
|
if (gext.Equals(extension, StringComparison.OrdinalIgnoreCase)) |
||||||
|
return g; |
||||||
|
} |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
public CodeSnippet FindSnippet(string extension, string name) |
||||||
|
{ |
||||||
|
CodeSnippetGroup g = FindGroup(extension); |
||||||
|
if (g != null) { |
||||||
|
return g.Snippets.FirstOrDefault(s => s.Name == name); |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,110 @@ |
|||||||
|
// <file>
|
||||||
|
// <copyright see="prj:///doc/copyright.txt"/>
|
||||||
|
// <license see="prj:///doc/license.txt"/>
|
||||||
|
// <owner name="Daniel Grunwald"/>
|
||||||
|
// <version>$Revision$</version>
|
||||||
|
// </file>
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Collections.ObjectModel; |
||||||
|
using System.Linq; |
||||||
|
using System.Text; |
||||||
|
using System.Windows; |
||||||
|
using System.Windows.Controls; |
||||||
|
using System.Windows.Data; |
||||||
|
using System.Windows.Documents; |
||||||
|
using System.Windows.Input; |
||||||
|
using System.Windows.Media; |
||||||
|
|
||||||
|
using ICSharpCode.Core; |
||||||
|
using ICSharpCode.SharpDevelop; |
||||||
|
|
||||||
|
namespace ICSharpCode.AvalonEdit.AddIn.Snippets |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Interaction logic for Snippets.xaml
|
||||||
|
/// </summary>
|
||||||
|
public partial class SnippetOptionPanel : UserControl, IOptionPanel |
||||||
|
{ |
||||||
|
ObservableCollection<CodeSnippetGroup> groups; |
||||||
|
|
||||||
|
public SnippetOptionPanel() |
||||||
|
{ |
||||||
|
InitializeComponent(); |
||||||
|
} |
||||||
|
|
||||||
|
public object Owner { get; set; } |
||||||
|
|
||||||
|
public object Control { |
||||||
|
get { return this; } |
||||||
|
} |
||||||
|
|
||||||
|
public void LoadOptions() |
||||||
|
{ |
||||||
|
groups = new ObservableCollection<CodeSnippetGroup>(SnippetManager.Instance.LoadGroups().OrderBy(g => g.Extensions)); |
||||||
|
extensionComboBox.ItemsSource = groups; |
||||||
|
extensionComboBox.SelectedItem = groups.FirstOrDefault(); |
||||||
|
} |
||||||
|
|
||||||
|
public bool SaveOptions() |
||||||
|
{ |
||||||
|
SnippetManager.Instance.SaveGroups(groups); |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
void AddGroupButton_Click(object sender, RoutedEventArgs e) |
||||||
|
{ |
||||||
|
string result = MessageService.ShowInputBox( |
||||||
|
"${res:Dialog.Options.CodeTemplate.AddGroupLabel}", |
||||||
|
"${res:Dialog.Options.CodeTemplate.EditGroupDialog.Text}", |
||||||
|
""); |
||||||
|
if (!string.IsNullOrEmpty(result)) { |
||||||
|
CodeSnippetGroup g = new CodeSnippetGroup(); |
||||||
|
g.Extensions = result; |
||||||
|
groups.Add(g); |
||||||
|
extensionComboBox.SelectedItem = g; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void RemoveGroupButton_Click(object sender, RoutedEventArgs e) |
||||||
|
{ |
||||||
|
if (extensionComboBox.SelectedIndex >= 0) |
||||||
|
groups.RemoveAt(extensionComboBox.SelectedIndex); |
||||||
|
} |
||||||
|
|
||||||
|
void EditGroupButton_Click(object sender, RoutedEventArgs e) |
||||||
|
{ |
||||||
|
CodeSnippetGroup g = (CodeSnippetGroup)extensionComboBox.SelectedItem; |
||||||
|
if (g != null) { |
||||||
|
string result = MessageService.ShowInputBox( |
||||||
|
"${res:Dialog.Options.CodeTemplate.EditGroupLabel}", |
||||||
|
"${res:Dialog.Options.CodeTemplate.EditGroupDialog.Text}", |
||||||
|
g.Extensions); |
||||||
|
if (!string.IsNullOrEmpty(result)) |
||||||
|
g.Extensions = result; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) |
||||||
|
{ |
||||||
|
CodeSnippet snippet = dataGrid.SelectedItem as CodeSnippet; |
||||||
|
if (snippet != null) { |
||||||
|
snippetTextBox.Text = snippet.Text; |
||||||
|
snippetTextBox.IsReadOnly = false; |
||||||
|
snippetTextBox.Background = SystemColors.WindowBrush; |
||||||
|
} else { |
||||||
|
snippetTextBox.Text = null; |
||||||
|
snippetTextBox.IsReadOnly = true; |
||||||
|
snippetTextBox.Background = SystemColors.ControlBrush; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void SnippetTextBox_TextChanged(object sender, EventArgs e) |
||||||
|
{ |
||||||
|
CodeSnippet snippet = dataGrid.SelectedItem as CodeSnippet; |
||||||
|
if (snippet != null) |
||||||
|
snippet.Text = snippetTextBox.Text; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,122 @@ |
|||||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||||
|
<UserControl |
||||||
|
x:Class="ICSharpCode.AvalonEdit.AddIn.Snippets.SnippetOptionPanel" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:core="http://icsharpcode.net/sharpdevelop/core" xmlns:AvalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"> |
||||||
|
<Grid> |
||||||
|
<Grid.RowDefinitions> |
||||||
|
<RowDefinition |
||||||
|
Height="Auto" /> |
||||||
|
<RowDefinition |
||||||
|
Height="1*" /> |
||||||
|
<RowDefinition |
||||||
|
Height="1*" /> |
||||||
|
</Grid.RowDefinitions> |
||||||
|
<Grid |
||||||
|
Grid.Column="0" |
||||||
|
Grid.Row="0" |
||||||
|
HorizontalAlignment="Stretch" |
||||||
|
VerticalAlignment="Top"> |
||||||
|
<Grid.RowDefinitions> |
||||||
|
<RowDefinition |
||||||
|
Height="Auto" /> |
||||||
|
<RowDefinition |
||||||
|
Height="Auto" /> |
||||||
|
</Grid.RowDefinitions> |
||||||
|
<Grid.ColumnDefinitions> |
||||||
|
<ColumnDefinition |
||||||
|
Width="Auto" /> |
||||||
|
<ColumnDefinition |
||||||
|
Width="*" /> |
||||||
|
</Grid.ColumnDefinitions> |
||||||
|
<Label |
||||||
|
Content="{core:StringParse ${res:Dialog.Options.CodeTemplate.ExtensionsLabel}:}" |
||||||
|
Grid.Column="0" |
||||||
|
Grid.Row="0" |
||||||
|
Target="{Binding ElementName=extensionComboBox}" /> |
||||||
|
<ComboBox |
||||||
|
Name="extensionComboBox" |
||||||
|
DisplayMemberPath="Extensions" |
||||||
|
Grid.Column="1" |
||||||
|
Grid.Row="0" |
||||||
|
Height="23" /> |
||||||
|
<StackPanel |
||||||
|
Grid.Column="0" |
||||||
|
Grid.ColumnSpan="2" |
||||||
|
Grid.Row="1" |
||||||
|
Orientation="Horizontal"> |
||||||
|
<Button |
||||||
|
Content="{core:Localize Dialog.Options.CodeTemplate.AddGroupLabel}" |
||||||
|
Margin="0,8,0,8" |
||||||
|
Height="23" |
||||||
|
Padding="9,1,9,1" |
||||||
|
Click="AddGroupButton_Click" /> |
||||||
|
<Button |
||||||
|
Content="{core:Localize Dialog.Options.CodeTemplate.RemoveGroupLabel}" |
||||||
|
Margin="8,8,0,8" |
||||||
|
Height="23" |
||||||
|
Padding="9,1,9,1" |
||||||
|
Click="RemoveGroupButton_Click" /> |
||||||
|
<Button |
||||||
|
Content="{core:Localize Dialog.Options.CodeTemplate.EditGroupLabel}" |
||||||
|
Margin="8,8,0,8" |
||||||
|
Height="23" |
||||||
|
Padding="9,1,9,1" |
||||||
|
Click="EditGroupButton_Click" /> |
||||||
|
</StackPanel> |
||||||
|
</Grid> |
||||||
|
<!--<DockPanel |
||||||
|
Grid.Row="1" |
||||||
|
Grid.Column="0"> |
||||||
|
<StackPanel |
||||||
|
DockPanel.Dock="Right" |
||||||
|
VerticalAlignment="Center"> |
||||||
|
<Button |
||||||
|
Content="{core:Localize Global.AddButtonText}" |
||||||
|
Height="23" |
||||||
|
Margin="4,4,4,4" |
||||||
|
Padding="9,1,9,1" |
||||||
|
Click="AddSnippetButton_Click" /> |
||||||
|
<Button |
||||||
|
Content="{core:Localize Global.EditButtonText}" |
||||||
|
Height="23" |
||||||
|
Margin="4,4,4,4" |
||||||
|
Padding="9,1,9,1" |
||||||
|
Click="EditSnippetButton_Click" /> |
||||||
|
<Button |
||||||
|
Content="{core:Localize Global.RemoveButtonText}" |
||||||
|
Height="23" |
||||||
|
Margin="4,4,4,4" |
||||||
|
Padding="9,1,9,1" |
||||||
|
Click="RemoveSnippetButton_Click" /> |
||||||
|
</StackPanel>--> |
||||||
|
<DataGrid |
||||||
|
Grid.Row="1" |
||||||
|
Grid.Column="0" |
||||||
|
x:Name="dataGrid" |
||||||
|
SelectionMode="Single" |
||||||
|
CanUserAddRows="True" |
||||||
|
CanUserDeleteRows="True" |
||||||
|
SelectionUnit="FullRow" |
||||||
|
HeadersVisibility="Column" |
||||||
|
CanUserResizeRows="False" |
||||||
|
AutoGenerateColumns="False" |
||||||
|
ItemsSource="{Binding ElementName=extensionComboBox, Path=SelectedItem.Snippets}" |
||||||
|
SelectionChanged="DataGrid_SelectionChanged"> |
||||||
|
<DataGrid.Columns> |
||||||
|
<DataGridTextColumn |
||||||
|
Header="{core:Localize Dialog.Options.CodeTemplate.Template}" |
||||||
|
Binding="{Binding Name}" /> |
||||||
|
<DataGridTextColumn |
||||||
|
Header="{core:Localize Dialog.Options.CodeTemplate.Description}" |
||||||
|
Width="*" |
||||||
|
Binding="{Binding Description}" /> |
||||||
|
</DataGrid.Columns> |
||||||
|
</DataGrid> |
||||||
|
<!--</DockPanel>--> |
||||||
|
<AvalonEdit:TextEditor |
||||||
|
x:Name="snippetTextBox" |
||||||
|
Grid.Column="0" |
||||||
|
Grid.Row="2" |
||||||
|
FontFamily="Consolas, Courier New" |
||||||
|
TextChanged="SnippetTextBox_TextChanged"/> |
||||||
|
</Grid> |
||||||
|
</UserControl> |
@ -1,108 +0,0 @@ |
|||||||
<Components version="1.0"> |
|
||||||
<System.Windows.Forms.UserControl> |
|
||||||
<Name value="MyUserControl" /> |
|
||||||
<DockPadding value="" /> |
|
||||||
<ClientSize value="{Width=320, Height=320}" /> |
|
||||||
<Controls> |
|
||||||
<System.Windows.Forms.Panel> |
|
||||||
<Name value="editorPanel" /> |
|
||||||
<Location value="{X=8,Y=192}" /> |
|
||||||
<Size value="{Width=304, Height=120}" /> |
|
||||||
<DockPadding value="" /> |
|
||||||
<Anchor value="Top, Bottom, Left, Right" /> |
|
||||||
<TabIndex value="8" /> |
|
||||||
<Controls> |
|
||||||
<System.Windows.Forms.TextBox> |
|
||||||
<Name value="templateTextBox" /> |
|
||||||
<TabIndex value="0" /> |
|
||||||
<Dock value="Fill" /> |
|
||||||
<Location value="{X=0,Y=0}" /> |
|
||||||
<AcceptsReturn value="True" /> |
|
||||||
<Size value="{Width=304, Height=120}" /> |
|
||||||
<Multiline value="True" /> |
|
||||||
<AcceptsTab value="True" /> |
|
||||||
<AutoSize value="False" /> |
|
||||||
<Text value="" /> |
|
||||||
<RightToLeft value="No" /> |
|
||||||
</System.Windows.Forms.TextBox> |
|
||||||
</Controls> |
|
||||||
</System.Windows.Forms.Panel> |
|
||||||
<System.Windows.Forms.Button> |
|
||||||
<Name value="removeButton" /> |
|
||||||
<Location value="{X=232,Y=160}" /> |
|
||||||
<Text value="${res:Global.RemoveButtonText}" /> |
|
||||||
<Anchor value="Top, Right" /> |
|
||||||
<TabIndex value="7" /> |
|
||||||
</System.Windows.Forms.Button> |
|
||||||
<System.Windows.Forms.Button> |
|
||||||
<Name value="editButton" /> |
|
||||||
<Location value="{X=232,Y=128}" /> |
|
||||||
<Text value="${res:Global.EditButtonText}" /> |
|
||||||
<Anchor value="Top, Right" /> |
|
||||||
<TabIndex value="6" /> |
|
||||||
</System.Windows.Forms.Button> |
|
||||||
<System.Windows.Forms.Button> |
|
||||||
<Name value="addButton" /> |
|
||||||
<Location value="{X=232,Y=96}" /> |
|
||||||
<Text value="${res:Global.AddButtonText}" /> |
|
||||||
<Anchor value="Top, Right" /> |
|
||||||
<TabIndex value="5" /> |
|
||||||
</System.Windows.Forms.Button> |
|
||||||
<System.Windows.Forms.ListView> |
|
||||||
<Name value="templateListView" /> |
|
||||||
<GridLines value="True" /> |
|
||||||
<Anchor value="Top, Left, Right" /> |
|
||||||
<TabIndex value="4" /> |
|
||||||
<View value="Details" /> |
|
||||||
<Sorting value="Ascending" /> |
|
||||||
<FullRowSelect value="True" /> |
|
||||||
<Location value="{X=8,Y=72}" /> |
|
||||||
<Size value="{Width=216, Height=112}" /> |
|
||||||
<HideSelection value="False" /> |
|
||||||
<RightToLeft value="No" /> |
|
||||||
<Columns> |
|
||||||
<System.Windows.Forms.ColumnHeader> |
|
||||||
<Width value="70" /> |
|
||||||
<Name value="columnHeader" /> |
|
||||||
<Text value="${res:Dialog.Options.CodeTemplate.Template}" /> |
|
||||||
</System.Windows.Forms.ColumnHeader> |
|
||||||
<System.Windows.Forms.ColumnHeader> |
|
||||||
<Width value="140" /> |
|
||||||
<Name value="columnHeader2" /> |
|
||||||
<Text value="${res:Dialog.Options.CodeTemplate.Description}" /> |
|
||||||
</System.Windows.Forms.ColumnHeader> |
|
||||||
</Columns> |
|
||||||
</System.Windows.Forms.ListView> |
|
||||||
<System.Windows.Forms.Button> |
|
||||||
<Name value="removeGroupButton" /> |
|
||||||
<Location value="{X=200,Y=40}" /> |
|
||||||
<Size value="{Width=96, Height=23}" /> |
|
||||||
<Text value="${res:Dialog.Options.CodeTemplate.RemoveGroupLabel}" /> |
|
||||||
<TabIndex value="3" /> |
|
||||||
</System.Windows.Forms.Button> |
|
||||||
<System.Windows.Forms.Button> |
|
||||||
<Name value="addGroupButton" /> |
|
||||||
<Location value="{X=88,Y=40}" /> |
|
||||||
<Size value="{Width=104, Height=23}" /> |
|
||||||
<Text value="${res:Dialog.Options.CodeTemplate.AddGroupLabel}" /> |
|
||||||
<TabIndex value="2" /> |
|
||||||
</System.Windows.Forms.Button> |
|
||||||
<System.Windows.Forms.ComboBox> |
|
||||||
<Name value="groupComboBox" /> |
|
||||||
<Anchor value="Top, Left, Right" /> |
|
||||||
<TabIndex value="1" /> |
|
||||||
<Location value="{X=88,Y=8}" /> |
|
||||||
<Size value="{Width=228, Height=21}" /> |
|
||||||
<RightToLeft value="No" /> |
|
||||||
</System.Windows.Forms.ComboBox> |
|
||||||
<System.Windows.Forms.Label> |
|
||||||
<Name value="extensionLabel" /> |
|
||||||
<Text value="${res:Dialog.Options.CodeTemplate.ExtensionsLabel}" /> |
|
||||||
<TextAlign value="MiddleRight" /> |
|
||||||
<TabIndex value="0" /> |
|
||||||
<Size value="{Width=80, Height=23}" /> |
|
||||||
<Location value="{X=8,Y=8}" /> |
|
||||||
</System.Windows.Forms.Label> |
|
||||||
</Controls> |
|
||||||
</System.Windows.Forms.UserControl> |
|
||||||
</Components> |
|
@ -1,69 +0,0 @@ |
|||||||
// <file>
|
|
||||||
// <copyright see="prj:///doc/copyright.txt"/>
|
|
||||||
// <license see="prj:///doc/license.txt"/>
|
|
||||||
// <owner name="Daniel Grunwald"/>
|
|
||||||
// <version>$Revision$</version>
|
|
||||||
// </file>
|
|
||||||
|
|
||||||
using System; |
|
||||||
using ICSharpCode.Core; |
|
||||||
using ICSharpCode.SharpDevelop.Internal.Templates; |
|
||||||
|
|
||||||
namespace ICSharpCode.SharpDevelop.Editor.CodeCompletion |
|
||||||
{ |
|
||||||
public class TemplateCompletionItemProvider : AbstractCompletionItemProvider |
|
||||||
{ |
|
||||||
public bool AutomaticInsert; |
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
public override ICompletionItemList GenerateCompletionList(ITextEditor editor) |
|
||||||
{ |
|
||||||
CodeTemplateGroup templateGroup = CodeTemplateLoader.GetTemplateGroupPerFilename(editor.FileName); |
|
||||||
if (templateGroup == null) { |
|
||||||
return null; |
|
||||||
} |
|
||||||
DefaultCompletionItemList result = new DefaultCompletionItemList(); |
|
||||||
bool automaticInsert = this.AutomaticInsert || DefaultEditor.Gui.Editor.SharpDevelopTextEditorProperties.Instance.AutoInsertTemplates; |
|
||||||
foreach (CodeTemplate template in templateGroup.Templates) { |
|
||||||
result.Items.Add(new TemplateCompletionItem(template)); |
|
||||||
} |
|
||||||
result.SortItems(); |
|
||||||
|
|
||||||
return result; |
|
||||||
} |
|
||||||
|
|
||||||
sealed class TemplateCompletionItem : ICompletionItem |
|
||||||
{ |
|
||||||
public readonly CodeTemplate template; |
|
||||||
|
|
||||||
public TemplateCompletionItem(CodeTemplate template) |
|
||||||
{ |
|
||||||
this.template = template; |
|
||||||
} |
|
||||||
|
|
||||||
public string Text { |
|
||||||
get { |
|
||||||
return template.Shortcut; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public string Description { |
|
||||||
get { |
|
||||||
return template.Description + StringParser.Parse("\n${res:Dialog.Options.CodeTemplate.PressTabToInsertTemplate}\n\n") |
|
||||||
+ template.Text; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public IImage Image { |
|
||||||
get { |
|
||||||
return ClassBrowserIconService.CodeTemplate; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public void Complete(CompletionContext context) |
|
||||||
{ |
|
||||||
context.Editor.Document.Replace(context.StartOffset, context.Length, template.Text); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -1,91 +0,0 @@ |
|||||||
// <file>
|
|
||||||
// <copyright see="prj:///doc/copyright.txt"/>
|
|
||||||
// <license see="prj:///doc/license.txt"/>
|
|
||||||
// <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
|
|
||||||
// <version>$Revision$</version>
|
|
||||||
// </file>
|
|
||||||
|
|
||||||
using System; |
|
||||||
using System.Xml; |
|
||||||
|
|
||||||
namespace ICSharpCode.SharpDevelop.Internal.Templates |
|
||||||
{ |
|
||||||
/// <summary>
|
|
||||||
/// This class reperesents a single Code Template
|
|
||||||
/// </summary>
|
|
||||||
public class CodeTemplate |
|
||||||
{ |
|
||||||
string shortcut = String.Empty; |
|
||||||
string description = String.Empty; |
|
||||||
string text = String.Empty; |
|
||||||
|
|
||||||
public string Shortcut { |
|
||||||
get { |
|
||||||
return shortcut; |
|
||||||
} |
|
||||||
set { |
|
||||||
shortcut = value; |
|
||||||
System.Diagnostics.Debug.Assert(shortcut != null, "ICSharpCode.SharpDevelop.Internal.Template : string Shortcut == null"); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public string Description { |
|
||||||
get { |
|
||||||
return description; |
|
||||||
} |
|
||||||
set { |
|
||||||
description = value; |
|
||||||
System.Diagnostics.Debug.Assert(description != null, "ICSharpCode.SharpDevelop.Internal.Template : string Description == null"); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public string Text { |
|
||||||
get { |
|
||||||
return text; |
|
||||||
} |
|
||||||
set { |
|
||||||
text = value; |
|
||||||
System.Diagnostics.Debug.Assert(text != null, "ICSharpCode.SharpDevelop.Internal.Template : string Text == null"); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public CodeTemplate() |
|
||||||
{ |
|
||||||
} |
|
||||||
|
|
||||||
public CodeTemplate(string shortcut, string description, string text) |
|
||||||
{ |
|
||||||
this.shortcut = shortcut; |
|
||||||
this.description = description; |
|
||||||
this.text = text; |
|
||||||
} |
|
||||||
|
|
||||||
public CodeTemplate(XmlElement el) |
|
||||||
{ |
|
||||||
if (el == null) { |
|
||||||
throw new ArgumentNullException("el"); |
|
||||||
} |
|
||||||
|
|
||||||
if (el.Attributes["template"] == null || el.Attributes["description"] == null) { |
|
||||||
throw new Exception("CodeTemplate(XmlElement el) : template and description attributes must exist (check the CodeTemplate XML)"); |
|
||||||
} |
|
||||||
|
|
||||||
Shortcut = el.GetAttribute("template"); |
|
||||||
Description = el.GetAttribute("description"); |
|
||||||
Text = el.InnerText; |
|
||||||
} |
|
||||||
|
|
||||||
public XmlElement ToXmlElement(XmlDocument doc) |
|
||||||
{ |
|
||||||
if (doc == null) { |
|
||||||
throw new ArgumentNullException("doc"); |
|
||||||
} |
|
||||||
|
|
||||||
XmlElement newElement = doc.CreateElement("CodeTemplate"); |
|
||||||
newElement.SetAttribute("template", Shortcut); |
|
||||||
newElement.SetAttribute("description", Description); |
|
||||||
newElement.InnerText = Text; |
|
||||||
return newElement; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -1,87 +0,0 @@ |
|||||||
// <file>
|
|
||||||
// <copyright see="prj:///doc/copyright.txt"/>
|
|
||||||
// <license see="prj:///doc/license.txt"/>
|
|
||||||
// <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
|
|
||||||
// <version>$Revision$</version>
|
|
||||||
// </file>
|
|
||||||
|
|
||||||
using System; |
|
||||||
using System.Collections.Generic; |
|
||||||
using System.Xml; |
|
||||||
|
|
||||||
namespace ICSharpCode.SharpDevelop.Internal.Templates |
|
||||||
{ |
|
||||||
/// <summary>
|
|
||||||
/// This class reperesents a single Code Template
|
|
||||||
/// </summary>
|
|
||||||
public class CodeTemplateGroup |
|
||||||
{ |
|
||||||
List<string> extensions = new List<string>(); |
|
||||||
List<CodeTemplate> templates = new List<CodeTemplate>(); |
|
||||||
|
|
||||||
public List<string> Extensions { |
|
||||||
get { |
|
||||||
return extensions; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public List<CodeTemplate> Templates { |
|
||||||
get { |
|
||||||
return templates; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public string[] ExtensionStrings { |
|
||||||
get { |
|
||||||
string[] extensionStrings = new string[extensions.Count]; |
|
||||||
extensions.CopyTo(extensionStrings, 0); |
|
||||||
return extensionStrings; |
|
||||||
} |
|
||||||
set { |
|
||||||
extensions.Clear(); |
|
||||||
foreach (string str in value) { |
|
||||||
extensions.Add(str); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public CodeTemplateGroup(string extensions) |
|
||||||
{ |
|
||||||
ExtensionStrings = extensions.Split(';'); |
|
||||||
} |
|
||||||
|
|
||||||
public CodeTemplateGroup(XmlElement el) |
|
||||||
{ |
|
||||||
if (el == null) { |
|
||||||
throw new ArgumentNullException("el"); |
|
||||||
} |
|
||||||
string[] exts = el.GetAttribute("extensions").Split(';'); |
|
||||||
foreach (string ext in exts) { |
|
||||||
extensions.Add(ext); |
|
||||||
} |
|
||||||
foreach (XmlNode childNode in el.ChildNodes) { |
|
||||||
XmlElement childElement = childNode as XmlElement; |
|
||||||
if (childElement != null) { |
|
||||||
templates.Add(new CodeTemplate(childElement)); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public XmlElement ToXmlElement(XmlDocument doc) |
|
||||||
{ |
|
||||||
if (doc == null) { |
|
||||||
throw new ArgumentNullException("doc"); |
|
||||||
} |
|
||||||
XmlElement newElement = doc.CreateElement("CodeTemplateGroup"); |
|
||||||
|
|
||||||
newElement.SetAttribute("extensions", String.Join(";", ExtensionStrings)); |
|
||||||
|
|
||||||
foreach (CodeTemplate codeTemplate in templates) { |
|
||||||
newElement.AppendChild(codeTemplate.ToXmlElement(doc)); |
|
||||||
} |
|
||||||
|
|
||||||
return newElement; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
} |
|
@ -1,119 +0,0 @@ |
|||||||
// <file>
|
|
||||||
// <copyright see="prj:///doc/copyright.txt"/>
|
|
||||||
// <license see="prj:///doc/license.txt"/>
|
|
||||||
// <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
|
|
||||||
// <version>$Revision$</version>
|
|
||||||
// </file>
|
|
||||||
|
|
||||||
using System; |
|
||||||
using System.Collections; |
|
||||||
using System.IO; |
|
||||||
using System.Xml; |
|
||||||
|
|
||||||
using ICSharpCode.Core; |
|
||||||
|
|
||||||
namespace ICSharpCode.SharpDevelop.Internal.Templates |
|
||||||
{ |
|
||||||
/// <summary>
|
|
||||||
/// This class handles the code templates
|
|
||||||
/// </summary>
|
|
||||||
public class CodeTemplateLoader |
|
||||||
{ |
|
||||||
static string TemplateFileName = "SharpDevelop-templates.xml"; |
|
||||||
static string TemplateVersion = "2.0"; |
|
||||||
|
|
||||||
static ArrayList templateGroups = new ArrayList(); |
|
||||||
|
|
||||||
public static ArrayList TemplateGroups { |
|
||||||
get { |
|
||||||
return templateGroups; |
|
||||||
} |
|
||||||
set { |
|
||||||
templateGroups = value; |
|
||||||
System.Diagnostics.Debug.Assert(templateGroups != null); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public static CodeTemplateGroup GetTemplateGroupPerFilename(string fileName) |
|
||||||
{ |
|
||||||
return GetTemplateGroupPerExtension(Path.GetExtension(fileName)); |
|
||||||
} |
|
||||||
public static CodeTemplateGroup GetTemplateGroupPerExtension(string extension) |
|
||||||
{ |
|
||||||
foreach (CodeTemplateGroup group in templateGroups) { |
|
||||||
foreach (string groupExtension in group.Extensions) { |
|
||||||
if (groupExtension == extension) { |
|
||||||
return group; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
return null; |
|
||||||
} |
|
||||||
|
|
||||||
static bool LoadTemplatesFromStream(string filename) |
|
||||||
{ |
|
||||||
if (!File.Exists(filename)) { |
|
||||||
return false; |
|
||||||
} |
|
||||||
|
|
||||||
XmlDocument doc = new XmlDocument(); |
|
||||||
try { |
|
||||||
doc.PreserveWhitespace = true; |
|
||||||
doc.Load(filename); |
|
||||||
|
|
||||||
templateGroups = new ArrayList(); |
|
||||||
|
|
||||||
if (doc.DocumentElement.GetAttribute("version") != TemplateVersion) { |
|
||||||
return false; |
|
||||||
} |
|
||||||
|
|
||||||
foreach (XmlNode n in doc.DocumentElement.ChildNodes) { |
|
||||||
XmlElement el = n as XmlElement; |
|
||||||
if (el != null) { |
|
||||||
templateGroups.Add(new CodeTemplateGroup(el)); |
|
||||||
} |
|
||||||
} |
|
||||||
} catch (FileNotFoundException) { |
|
||||||
return false; |
|
||||||
} |
|
||||||
return true; |
|
||||||
} |
|
||||||
|
|
||||||
static void WriteTemplatesToFile(string fileName) |
|
||||||
{ |
|
||||||
XmlDocument doc = new XmlDocument(); |
|
||||||
|
|
||||||
doc.LoadXml("<CodeTemplates version = \"" + TemplateVersion + "\" />"); |
|
||||||
|
|
||||||
foreach (CodeTemplateGroup codeTemplateGroup in templateGroups) { |
|
||||||
doc.DocumentElement.AppendChild(codeTemplateGroup.ToXmlElement(doc)); |
|
||||||
} |
|
||||||
|
|
||||||
FileUtility.ObservedSave(new NamedFileOperationDelegate(doc.Save), fileName, FileErrorPolicy.ProvideAlternative); |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// This method loads the code templates from a XML based
|
|
||||||
/// configuration file.
|
|
||||||
/// </summary>
|
|
||||||
static CodeTemplateLoader() |
|
||||||
{ |
|
||||||
if (!LoadTemplatesFromStream(Path.Combine(PropertyService.ConfigDirectory, TemplateFileName))) { |
|
||||||
LoggingService.Info("Templates: can't load user defaults, reading system defaults"); |
|
||||||
if (!LoadTemplatesFromStream(FileUtility.Combine(PropertyService.DataDirectory, "options", TemplateFileName))) { |
|
||||||
MessageService.ShowWarning("${res:Internal.Templates.CodeTemplateLoader.CantLoadTemplatesWarning}"); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// This method saves the code templates to a XML based
|
|
||||||
/// configuration file in the current user's own files directory
|
|
||||||
/// </summary>
|
|
||||||
public static void SaveTemplates() |
|
||||||
{ |
|
||||||
|
|
||||||
WriteTemplatesToFile(Path.Combine(PropertyService.ConfigDirectory, TemplateFileName)); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -1,262 +0,0 @@ |
|||||||
// <file>
|
|
||||||
// <copyright see="prj:///doc/copyright.txt"/>
|
|
||||||
// <license see="prj:///doc/license.txt"/>
|
|
||||||
// <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
|
|
||||||
// <version>$Revision$</version>
|
|
||||||
// </file>
|
|
||||||
|
|
||||||
using System; |
|
||||||
using System.Collections; |
|
||||||
using System.Windows.Forms; |
|
||||||
|
|
||||||
using ICSharpCode.Core.WinForms; |
|
||||||
using ICSharpCode.SharpDevelop.Internal.Templates; |
|
||||||
|
|
||||||
namespace ICSharpCode.SharpDevelop.Gui.OptionPanels |
|
||||||
{ |
|
||||||
public class CodeTemplatePanel : XmlFormsOptionPanel |
|
||||||
{ |
|
||||||
ArrayList templateGroups; |
|
||||||
int currentSelectedGroup = -1; |
|
||||||
|
|
||||||
public CodeTemplateGroup CurrentTemplateGroup { |
|
||||||
get { |
|
||||||
if (currentSelectedGroup < 0 || currentSelectedGroup >= templateGroups.Count) { |
|
||||||
return null; |
|
||||||
} |
|
||||||
return (CodeTemplateGroup)templateGroups[currentSelectedGroup]; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public override void LoadPanelContents() |
|
||||||
{ |
|
||||||
templateGroups = CopyCodeTemplateGroups(CodeTemplateLoader.TemplateGroups); |
|
||||||
|
|
||||||
SetupFromXmlStream(this.GetType().Assembly.GetManifestResourceStream("Resources.CodeTemplatePanel.xfrm")); |
|
||||||
|
|
||||||
ControlDictionary["removeButton"].Click += new System.EventHandler(RemoveEvent); |
|
||||||
ControlDictionary["addButton"].Click += new System.EventHandler(AddEvent); |
|
||||||
ControlDictionary["editButton"].Click += new System.EventHandler(EditEvent); |
|
||||||
|
|
||||||
ControlDictionary["addGroupButton"].Click += new System.EventHandler(AddGroupEvent); |
|
||||||
ControlDictionary["removeGroupButton"].Click += new System.EventHandler(RemoveGroupEvent); |
|
||||||
|
|
||||||
|
|
||||||
((TextBox)ControlDictionary["templateTextBox"]).Font = WinFormsResourceService.DefaultMonospacedFont; |
|
||||||
((TextBox)ControlDictionary["templateTextBox"]).TextChanged += new EventHandler(TextChange); |
|
||||||
|
|
||||||
((ListView)ControlDictionary["templateListView"]).Activation = ItemActivation.Standard; |
|
||||||
((ListView)ControlDictionary["templateListView"]).ItemActivate += new System.EventHandler(EditEvent); |
|
||||||
((ListView)ControlDictionary["templateListView"]).SelectedIndexChanged += new System.EventHandler(IndexChange); |
|
||||||
|
|
||||||
((ComboBox)ControlDictionary["groupComboBox"]).DropDown += new EventHandler(FillGroupBoxEvent); |
|
||||||
|
|
||||||
if (templateGroups.Count > 0) { |
|
||||||
currentSelectedGroup = 0; |
|
||||||
} |
|
||||||
|
|
||||||
FillGroupComboBox(); |
|
||||||
BuildListView(); |
|
||||||
IndexChange(null, null); |
|
||||||
SetEnabledStatus(); |
|
||||||
} |
|
||||||
|
|
||||||
public override bool StorePanelContents() |
|
||||||
{ |
|
||||||
CodeTemplateLoader.TemplateGroups = templateGroups; |
|
||||||
CodeTemplateLoader.SaveTemplates(); |
|
||||||
return true; |
|
||||||
} |
|
||||||
|
|
||||||
void FillGroupBoxEvent(object sender, EventArgs e) |
|
||||||
{ |
|
||||||
FillGroupComboBox(); |
|
||||||
} |
|
||||||
|
|
||||||
void SetEnabledStatus() |
|
||||||
{ |
|
||||||
bool groupSelected = CurrentTemplateGroup != null; |
|
||||||
bool groupsEmpty = templateGroups.Count != 0; |
|
||||||
|
|
||||||
SetEnabledStatus(groupSelected, "addButton", "editButton", "removeButton", "templateListView", "templateTextBox"); |
|
||||||
SetEnabledStatus(groupsEmpty, "groupComboBox", "extensionLabel"); |
|
||||||
if (groupSelected) { |
|
||||||
bool oneItemSelected = ((ListView)ControlDictionary["templateListView"]).SelectedItems.Count == 1; |
|
||||||
bool isItemSelected = ((ListView)ControlDictionary["templateListView"]).SelectedItems.Count > 0; |
|
||||||
SetEnabledStatus(oneItemSelected, "editButton", "templateTextBox"); |
|
||||||
SetEnabledStatus(isItemSelected, "removeButton"); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
#region GroupComboBox event handler
|
|
||||||
void SetGroupSelection(object sender, EventArgs e) |
|
||||||
{ |
|
||||||
currentSelectedGroup = ((ComboBox)ControlDictionary["groupComboBox"]).SelectedIndex; |
|
||||||
BuildListView(); |
|
||||||
} |
|
||||||
|
|
||||||
void GroupComboBoxTextChanged(object sender, EventArgs e) |
|
||||||
{ |
|
||||||
if (((ComboBox)ControlDictionary["groupComboBox"]).SelectedIndex >= 0) { |
|
||||||
currentSelectedGroup = ((ComboBox)ControlDictionary["groupComboBox"]).SelectedIndex; |
|
||||||
} |
|
||||||
if (CurrentTemplateGroup != null) { |
|
||||||
CurrentTemplateGroup.ExtensionStrings = ((ComboBox)ControlDictionary["groupComboBox"]).Text.Split(';'); |
|
||||||
} |
|
||||||
} |
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Group Button events
|
|
||||||
void AddGroupEvent(object sender, EventArgs e) |
|
||||||
{ |
|
||||||
templateGroups.Add(new CodeTemplateGroup(".???")); |
|
||||||
FillGroupComboBox(); |
|
||||||
((ComboBox)ControlDictionary["groupComboBox"]).SelectedIndex = templateGroups.Count - 1; |
|
||||||
SetEnabledStatus(); |
|
||||||
} |
|
||||||
|
|
||||||
void RemoveGroupEvent(object sender, EventArgs e) |
|
||||||
{ |
|
||||||
if (CurrentTemplateGroup != null) { |
|
||||||
templateGroups.RemoveAt(currentSelectedGroup); |
|
||||||
if (templateGroups.Count == 0) { |
|
||||||
currentSelectedGroup = -1; |
|
||||||
} else { |
|
||||||
((ComboBox)ControlDictionary["groupComboBox"]).SelectedIndex = Math.Min(currentSelectedGroup, templateGroups.Count - 1); |
|
||||||
} |
|
||||||
FillGroupComboBox(); |
|
||||||
BuildListView(); |
|
||||||
SetEnabledStatus(); |
|
||||||
} |
|
||||||
} |
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Template Button events
|
|
||||||
void RemoveEvent(object sender, System.EventArgs e) |
|
||||||
{ |
|
||||||
object[] selectedItems = new object[((ListView)ControlDictionary["templateListView"]).SelectedItems.Count]; |
|
||||||
((ListView)ControlDictionary["templateListView"]).SelectedItems.CopyTo(selectedItems, 0); |
|
||||||
|
|
||||||
foreach (ListViewItem item in selectedItems) { |
|
||||||
((ListView)ControlDictionary["templateListView"]).Items.Remove(item); |
|
||||||
} |
|
||||||
StoreTemplateGroup(); |
|
||||||
} |
|
||||||
|
|
||||||
void AddEvent(object sender, System.EventArgs e) |
|
||||||
{ |
|
||||||
CodeTemplate newTemplate = new CodeTemplate(); |
|
||||||
using (EditTemplateDialog etd = new EditTemplateDialog(newTemplate)) { |
|
||||||
if (etd.ShowDialog(WorkbenchSingleton.MainWin32Window) == DialogResult.OK) { |
|
||||||
CurrentTemplateGroup.Templates.Add(newTemplate); |
|
||||||
((ListView)ControlDictionary["templateListView"]).SelectedItems.Clear(); |
|
||||||
BuildListView(); |
|
||||||
((ListView)ControlDictionary["templateListView"]).Select(); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
void EditEvent(object sender, System.EventArgs e) |
|
||||||
{ |
|
||||||
int i = GetCurrentIndex(); |
|
||||||
if (i != -1) { |
|
||||||
ListViewItem item = ((ListView)ControlDictionary["templateListView"]).SelectedItems[0]; |
|
||||||
CodeTemplate template = (CodeTemplate)item.Tag; |
|
||||||
template = new CodeTemplate(template.Shortcut, template.Description, template.Text); |
|
||||||
|
|
||||||
using (EditTemplateDialog etd = new EditTemplateDialog(template)) { |
|
||||||
if (etd.ShowDialog(WorkbenchSingleton.MainWin32Window) == DialogResult.OK) { |
|
||||||
item.Tag = template; |
|
||||||
StoreTemplateGroup(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
BuildListView(); |
|
||||||
} |
|
||||||
} |
|
||||||
#endregion
|
|
||||||
|
|
||||||
void FillGroupComboBox() |
|
||||||
{ |
|
||||||
((ComboBox)ControlDictionary["groupComboBox"]).TextChanged -= new EventHandler(GroupComboBoxTextChanged); |
|
||||||
((ComboBox)ControlDictionary["groupComboBox"]).SelectedIndexChanged -= new EventHandler(SetGroupSelection); |
|
||||||
|
|
||||||
((ComboBox)ControlDictionary["groupComboBox"]).Items.Clear(); |
|
||||||
foreach (CodeTemplateGroup templateGroup in templateGroups) { |
|
||||||
((ComboBox)ControlDictionary["groupComboBox"]).Items.Add(String.Join(";", templateGroup.ExtensionStrings)); |
|
||||||
} |
|
||||||
((ComboBox)ControlDictionary["groupComboBox"]).Text = CurrentTemplateGroup != null ? ((ComboBox)ControlDictionary["groupComboBox"]).Items[currentSelectedGroup].ToString() : String.Empty; |
|
||||||
if (currentSelectedGroup >= 0) { |
|
||||||
((ComboBox)ControlDictionary["groupComboBox"]).SelectedIndex = currentSelectedGroup; |
|
||||||
} |
|
||||||
|
|
||||||
((ComboBox)ControlDictionary["groupComboBox"]).SelectedIndexChanged += new EventHandler(SetGroupSelection); |
|
||||||
((ComboBox)ControlDictionary["groupComboBox"]).TextChanged += new EventHandler(GroupComboBoxTextChanged); |
|
||||||
} |
|
||||||
|
|
||||||
int GetCurrentIndex() |
|
||||||
{ |
|
||||||
if (((ListView)ControlDictionary["templateListView"]).SelectedItems.Count == 1) { |
|
||||||
return ((ListView)ControlDictionary["templateListView"]).SelectedItems[0].Index; |
|
||||||
} |
|
||||||
return -1; |
|
||||||
} |
|
||||||
|
|
||||||
void IndexChange(object sender, System.EventArgs e) |
|
||||||
{ |
|
||||||
int i = GetCurrentIndex(); |
|
||||||
|
|
||||||
if (i != -1) { |
|
||||||
ControlDictionary["templateTextBox"].Text = ((CodeTemplate)((ListView)ControlDictionary["templateListView"]).SelectedItems[0].Tag).Text; |
|
||||||
} else { |
|
||||||
ControlDictionary["templateTextBox"].Text = String.Empty; |
|
||||||
} |
|
||||||
SetEnabledStatus(); |
|
||||||
} |
|
||||||
|
|
||||||
void TextChange(object sender, EventArgs e) |
|
||||||
{ |
|
||||||
int i = GetCurrentIndex(); |
|
||||||
if (i != -1) { |
|
||||||
((CodeTemplate)((ListView)ControlDictionary["templateListView"]).SelectedItems[0].Tag).Text = ControlDictionary["templateTextBox"].Text; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
void StoreTemplateGroup() |
|
||||||
{ |
|
||||||
if (CurrentTemplateGroup != null) { |
|
||||||
CurrentTemplateGroup.Templates.Clear(); |
|
||||||
foreach (ListViewItem item in ((ListView)ControlDictionary["templateListView"]).Items) { |
|
||||||
CurrentTemplateGroup.Templates.Add((CodeTemplate)item.Tag); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
void BuildListView() |
|
||||||
{ |
|
||||||
((ListView)ControlDictionary["templateListView"]).Items.Clear(); |
|
||||||
if (CurrentTemplateGroup != null) { |
|
||||||
foreach (CodeTemplate template in CurrentTemplateGroup.Templates) { |
|
||||||
ListViewItem newItem = new ListViewItem(new string[] { template.Shortcut, template.Description }); |
|
||||||
newItem.Tag = template; |
|
||||||
((ListView)ControlDictionary["templateListView"]).Items.Add(newItem); |
|
||||||
} |
|
||||||
} |
|
||||||
IndexChange(this, EventArgs.Empty); |
|
||||||
} |
|
||||||
|
|
||||||
ArrayList CopyCodeTemplateGroups(ArrayList groups) |
|
||||||
{ |
|
||||||
ArrayList copiedGroups = new ArrayList(); |
|
||||||
foreach (CodeTemplateGroup group in groups) { |
|
||||||
CodeTemplateGroup newGroup = new CodeTemplateGroup(String.Join(";", group.ExtensionStrings)); |
|
||||||
foreach (CodeTemplate template in group.Templates) { |
|
||||||
CodeTemplate newTemplate = new CodeTemplate(template.Shortcut, template.Description, template.Text); |
|
||||||
newGroup.Templates.Add(newTemplate); |
|
||||||
} |
|
||||||
copiedGroups.Add(newGroup); |
|
||||||
} |
|
||||||
return copiedGroups; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
Loading…
Reference in new issue