diff options
author | Mike Krüger <mkrueger@xamarin.com> | 2016-07-27 10:17:29 +0300 |
---|---|---|
committer | Mike Krüger <mkrueger@xamarin.com> | 2016-07-27 10:17:29 +0300 |
commit | f6b05bc85d422dfdab1cd3b8ad7bb82cc68b37fe (patch) | |
tree | 98f5beae7d48fb5080c2bbfbfd9e54ad6778f5dc /main/src/core/MonoDevelop.Ide | |
parent | 6cf91e3181a6434b6cd9693ff18580d139815d75 (diff) |
[Ide] Added text mate language wrapper class.
It's for extracting preferences out of the text mate settings.
Diffstat (limited to 'main/src/core/MonoDevelop.Ide')
5 files changed, 158 insertions, 67 deletions
diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Editor.Extension/DefaultCommandTextEditorExtension.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Editor.Extension/DefaultCommandTextEditorExtension.cs index c933f5b181..449fc65d5d 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Editor.Extension/DefaultCommandTextEditorExtension.cs +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Editor.Extension/DefaultCommandTextEditorExtension.cs @@ -32,6 +32,7 @@ using MonoDevelop.Core.Text; using MonoDevelop.Ide.Commands; using MonoDevelop.Ide.TypeSystem; using MonoDevelop.Ide.Editor.Highlighting; +using MonoDevelop.Ide.Editor.TextMate; namespace MonoDevelop.Ide.Editor.Extension { @@ -41,10 +42,10 @@ namespace MonoDevelop.Ide.Editor.Extension void ToggleCodeCommentWithBlockComments () { var scope = Editor.SyntaxHighlighting.GetLinStartScopeStack (Editor.GetLine (Editor.CaretLine)); - string lineComment = null; - List<string> blockStarts = new List<string> (); - List<string> blockEnds = new List<string> (); - GetCommentTags (scope, ref lineComment, blockStarts, blockEnds); + var lang = TextMateLanguage.Create (scope); + var lineComments = lang.LineComments.ToArray (); + var blockStarts = lang.BlockComments.Select (b => b.Item1).ToList (); + var blockEnds = lang.BlockComments.Select (b => b.Item2).ToList (); if (blockStarts.Count == 0 || blockEnds.Count == 0) return; @@ -88,16 +89,13 @@ namespace MonoDevelop.Ide.Editor.Extension bool TryGetLineCommentTag (out string commentTag) { var scope = Editor.SyntaxHighlighting.GetLinStartScopeStack (Editor.GetLine (Editor.CaretLine)); - string lineComment = null; - List<string> blockStarts = new List<string> (); - List<string> blockEnds = new List<string> (); - GetCommentTags (scope, ref lineComment, blockStarts, blockEnds); + var lang = TextMateLanguage.Create (scope); - if (lineComment == null) { + if (lang.LineComments.Count == 0) { commentTag = null; return false; } - commentTag = lineComment; + commentTag = lang.LineComments [0]; return true; } @@ -107,32 +105,8 @@ namespace MonoDevelop.Ide.Editor.Extension void OnUpdateToggleComment (CommandInfo info) { var scope = Editor.SyntaxHighlighting.GetLinStartScopeStack (Editor.GetLine (Editor.CaretLine)); - string lineComment = null; - List<string> blockStarts = new List<string> (); - List<string> blockEnds = new List<string> (); - GetCommentTags (scope, ref lineComment, blockStarts, blockEnds); - - if (lineComment != null) { - info.Visible = true; - return; - } - info.Visible = blockStarts != null && blockStarts.Count > 0 && blockEnds != null && blockEnds.Count > 0; - } - - internal static void GetCommentTags (System.Collections.Immutable.ImmutableStack<string> scope, ref string lineComment, List<string> blockStarts, List<string> blockEnds) - {
- foreach (var setting in SyntaxHighlightingService.GetSettings (scope).Where (s => s.Settings.ContainsKey ("shellVariables"))) { - var vars = (PArray)setting.Settings ["shellVariables"]; - foreach (var d in vars.OfType<PDictionary> ()) { - var name = d.Get<PString> ("name").Value; - if (name == "TM_COMMENT_START") - lineComment = d.Get<PString> ("value").Value; - if (name == "TM_COMMENT_START_2") - blockStarts.Add (d.Get<PString> ("value").Value); - if (name == "TM_COMMENT_END_2") - blockEnds.Add (d.Get<PString> ("value").Value); - } - } + var lang = TextMateLanguage.Create (scope); + info.Visible = lang.LineComments.Count + lang.BlockComments.Count > 0; } [CommandHandler (EditCommands.ToggleCodeComment)] diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Editor.TextMate/TextMateLanguage.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Editor.TextMate/TextMateLanguage.cs new file mode 100644 index 0000000000..14c1fb83c1 --- /dev/null +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Editor.TextMate/TextMateLanguage.cs @@ -0,0 +1,122 @@ +// +// TextMateLanguage.cs +// +// Author: +// Mike Krüger <mikkrg@microsoft.com> +// +// Copyright (c) 2016 Microsoft Corporation +// +// 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 MonoDevelop.Ide.Editor.Extension; +using MonoDevelop.Ide.Editor.Highlighting; +using MonoDevelop.Ide.Editor.Highlighting.RegexEngine; +using System.Collections.Generic; +using System.Linq; + +namespace MonoDevelop.Ide.Editor.TextMate +{ + public class TextMateLanguage + { + readonly System.Collections.Immutable.ImmutableStack<string> scope; + + Dictionary<string, string> shellVariables; + Dictionary<string, string> ShellVariables { + get { + if (shellVariables != null) + return shellVariables; + shellVariables = new Dictionary<string, string> (); + foreach (var setting in SyntaxHighlightingService.GetSettings (scope).Where (s => s.Settings.ContainsKey ("shellVariables"))) { + var vars = (PArray)setting.Settings ["shellVariables"]; + foreach (var d in vars.OfType<PDictionary> ()) { + var name = d.Get<PString> ("name").Value; + shellVariables [name] = d.Get<PString> ("value").Value; + } + } + return shellVariables; + } + } + + + internal IEnumerable<TmSnippet> Snippets { + get { + return SyntaxHighlightingService.GetSnippets (scope); + } + } + + string GetCommentStartString (int num) + { + if (num > 1) + return "TM_COMMENT_START_" + (num + 1); + return "TM_COMMENT_START"; + } + + string GetCommentEndString (int num) + { + if (num > 1) + return "TM_COMMENT_END_" + (num + 1); + return "TM_COMMENT_END"; + } + + List<string> lineComments; + public IReadOnlyList<string> LineComments { + get { + if (lineComments != null) + return lineComments; + ExtractComments (); + return lineComments; + } + } + + List<Tuple<string, string>> blockComments; + public IReadOnlyList<Tuple<string, string>> BlockComments { + get { + if (blockComments != null) + return blockComments; + ExtractComments (); + return blockComments; + } + } + + void ExtractComments () + { + lineComments = new List<string> (); + blockComments = new List<Tuple<string, string>> (); + int i = 0; + while (true) { + string start, end; + if (!ShellVariables.TryGetValue (GetCommentStartString (i), out start)) + break; + if (ShellVariables.TryGetValue (GetCommentEndString (i), out end)) { + blockComments.Add (Tuple.Create (start, end)); + } else { + lineComments.Add (start); + } + } + } + + TextMateLanguage (System.Collections.Immutable.ImmutableStack<string> scope) + { + this.scope = scope; + } + + public static TextMateLanguage Create (System.Collections.Immutable.ImmutableStack<string> scope) => new TextMateLanguage (scope); + } +}
\ No newline at end of file diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Editor.Util/SimpleBracketMatcher.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Editor.Util/SimpleBracketMatcher.cs index f091ef6d6d..0c7801d908 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Editor.Util/SimpleBracketMatcher.cs +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Editor.Util/SimpleBracketMatcher.cs @@ -28,6 +28,8 @@ using System.Collections.Generic; using MonoDevelop.Ide.Editor.Highlighting; using System.Threading; using MonoDevelop.Ide.Editor.Extension; +using MonoDevelop.Ide.Editor.TextMate; +using System.Linq; namespace MonoDevelop.Ide.Editor.Util { @@ -80,12 +82,10 @@ namespace MonoDevelop.Ide.Editor.Util bool startsInLineComment = StartsInLineComment (document, offset); - - string lineComment = null; - List<string> blockCommentStarts = new List<string> (); - List<string> blockCommentEnds = new List<string> (); - DefaultCommandTextEditorExtension.GetCommentTags (SyntaxHighlightingService.GetScopeForFileName (document.FileName), ref lineComment, blockCommentStarts, blockCommentEnds); - string [] lineComments = lineComment != null ? new string [] { lineComment } : new string [0]; + var lang = TextMateLanguage.Create (SyntaxHighlightingService.GetScopeForFileName (document.FileName)); + var lineComments = lang.LineComments.ToArray (); + var blockCommentStarts = lang.BlockComments.Select (b => b.Item1).ToList (); + var blockCommentEnds = lang.BlockComments.Select (b => b.Item2).ToList (); var stringQuotes = new string [] { "\"", "'" }; int depth = -1; @@ -147,11 +147,10 @@ namespace MonoDevelop.Ide.Editor.Util static bool StartsInLineComment (IReadonlyTextDocument document, int offset) { - string lineComment = null; - List<string> blockCommentStarts = new List<string> (); - List<string> blockCommentEnds = new List<string> (); - DefaultCommandTextEditorExtension.GetCommentTags (SyntaxHighlightingService.GetScopeForFileName (document.FileName), ref lineComment, blockCommentStarts, blockCommentEnds); - string [] lineComments = lineComment != null ? new string [] { lineComment } : new string [0]; + var lang = TextMateLanguage.Create (SyntaxHighlightingService.GetScopeForFileName (document.FileName)); + var lineComments = lang.LineComments.ToArray (); + var blockCommentStarts = lang.BlockComments.Select (b => b.Item1).ToList (); + var blockCommentEnds = lang.BlockComments.Select (b => b.Item2).ToList (); var line = document.GetLineByOffset (offset); for (int i = line.Offset; i < offset; i++) { @@ -168,11 +167,10 @@ namespace MonoDevelop.Ide.Editor.Util bool isInLineComment = false; int curStringQuote = -1; - string lineComment = null; - List<string> blockCommentStarts = new List<string> (); - List<string> blockCommentEnds = new List<string> (); - DefaultCommandTextEditorExtension.GetCommentTags (SyntaxHighlightingService.GetScopeForFileName (document.FileName), ref lineComment, blockCommentStarts, blockCommentEnds); - string [] lineComments = lineComment != null ? new string [] { lineComment } : new string [0]; + var lang = TextMateLanguage.Create (SyntaxHighlightingService.GetScopeForFileName (document.FileName)); + var lineComments = lang.LineComments.ToArray (); + var blockCommentStarts = lang.BlockComments.Select (b => b.Item1).ToList (); + var blockCommentEnds = lang.BlockComments.Select (b => b.Item2).ToList (); var stringQuotes = new string [] { "\"", "'" }; @@ -213,12 +211,10 @@ namespace MonoDevelop.Ide.Editor.Util bool isInBlockComment = false; bool isInLineComment = false; int curStringQuote = -1; - - string lineComment = null; - List<string> blockCommentStarts = new List<string> (); - List<string> blockCommentEnds = new List<string> (); - DefaultCommandTextEditorExtension.GetCommentTags (SyntaxHighlightingService.GetScopeForFileName (document.FileName), ref lineComment, blockCommentStarts, blockCommentEnds); - string [] lineComments = lineComment != null ? new string [] { lineComment } : new string [0]; + var lang = TextMateLanguage.Create (SyntaxHighlightingService.GetScopeForFileName (document.FileName)); + var lineComments = lang.LineComments; + var blockCommentStarts = lang.BlockComments.Select (b => b.Item1).ToList (); + var blockCommentEnds = lang.BlockComments.Select (b => b.Item2).ToList (); var stringQuotes = new string [] { "\"", "'" }; diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Gui/Document.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Gui/Document.cs index 4d0a09f8f1..e75fe60988 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Gui/Document.cs +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Gui/Document.cs @@ -58,6 +58,7 @@ using MonoDevelop.Components.Extensions; using MonoDevelop.Projects.SharedAssetsProjects; using MonoDevelop.Ide.Editor.Extension; using System.Collections.Immutable; +using MonoDevelop.Ide.Editor.TextMate; namespace MonoDevelop.Ide.Gui { @@ -1029,17 +1030,14 @@ namespace MonoDevelop.Ide.Gui //Document doc = IdeApp.Workbench.ActiveDocument; string loadedMimeType = DesktopService.GetMimeTypeForUri (fileName); - - string lineComment = null; - List<string> start = new List<string> (); - List<string> end = new List<string> (); - DefaultCommandTextEditorExtension.GetCommentTags (SyntaxHighlightingService.GetScopeForFileName (fileName), ref lineComment, start, end); - if (lineComment != null) - return new string[] { lineComment }; + var lang = TextMateLanguage.Create (SyntaxHighlightingService.GetScopeForFileName (fileName)); + + if (lang.LineComments.Count > 0) + return lang.LineComments.ToArray (); - if (start.Count > 0 && end.Count > 0) - return new [] { start[0], end[0] }; + if (lang.BlockComments.Count> 0) + return new [] { lang.BlockComments[0].Item1, lang.BlockComments[0].Item2 }; return null; } diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.csproj b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.csproj index 3505bbecda..0eaa88601a 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.csproj +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.csproj @@ -8631,6 +8631,7 @@ <Compile Include="MonoDevelop.Ide.Editor.TextMate\TextMateIndentationTracker.cs" /> <Compile Include="MonoDevelop.Ide.Editor.TextMate\IIndentEngine.cs" /> <Compile Include="MonoDevelop.Ide.Editor.TextMate\TextMateDocumentIndentEngine.cs" /> + <Compile Include="MonoDevelop.Ide.Editor.TextMate\TextMateLanguage.cs" /> </ItemGroup> <ItemGroup> <None Include="Makefile.am" /> |