diff options
author | Kirill Osenkov <KirillOsenkov@users.noreply.github.com> | 2018-08-31 21:22:16 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-31 21:22:16 +0300 |
commit | a352df55f11e8f7a381683ff1f86669acdf05f64 (patch) | |
tree | 4a8c540b432ed0a4c5b92bf109bb11f8c47d4bf3 | |
parent | 2f51ef0264e819952a316289eb7da325a7ef24ad (diff) | |
parent | 983ac0795af8d3d4db397396cb967f9f0c2362b0 (diff) |
Merge pull request #5823 from mono/master-vsts634581
Fixes VSTS Bug 634581: JSON IntelliSense inserts three quotes where
2 files changed, 61 insertions, 55 deletions
diff --git a/main/src/addins/MonoDevelop.SourceEditor2/MonoDevelop.SourceEditor/DefaultAutoInsertBracketHandler.cs b/main/src/addins/MonoDevelop.SourceEditor2/MonoDevelop.SourceEditor/DefaultAutoInsertBracketHandler.cs index 4f70214733..5e1873e8f5 100644 --- a/main/src/addins/MonoDevelop.SourceEditor2/MonoDevelop.SourceEditor/DefaultAutoInsertBracketHandler.cs +++ b/main/src/addins/MonoDevelop.SourceEditor2/MonoDevelop.SourceEditor/DefaultAutoInsertBracketHandler.cs @@ -24,11 +24,14 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Threading; using Mono.TextEditor.Highlighting; +using MonoDevelop.Core.Text; using MonoDevelop.Ide; using MonoDevelop.Ide.Editor; using MonoDevelop.Ide.Editor.Extension; +using System.Linq; namespace MonoDevelop.SourceEditor { @@ -37,25 +40,28 @@ namespace MonoDevelop.SourceEditor const string openBrackets = "{[('\""; const string closingBrackets = "}])'\""; + static readonly string[] excludedMimeTypes = { + "text/fsharp", "text/x-csharp", "text/x-json" + }; + public override bool Handle (TextEditor editor, DocumentContext ctx, KeyDescriptor descriptor) { - if (descriptor.KeyChar == '\'' && editor.MimeType == "text/fsharp") + if (descriptor.KeyChar == '\'') return false; - if (editor.MimeType == "text/x-csharp") + if (Array.IndexOf (excludedMimeTypes, editor.MimeType) < 0) return false; int braceIndex = openBrackets.IndexOf (descriptor.KeyChar); if (braceIndex < 0) return false; - - var extEditor = ((SourceEditorView)editor.Implementation).SourceEditorWidget.TextEditor; - var line = extEditor.Document.GetLine (extEditor.Caret.Line); + + var line = editor.GetLine (editor.CaretLine); if (line == null) return false; bool inStringOrComment = false; - var stack = extEditor.SyntaxHighlighting.GetScopeStackAsync (Math.Max (0, extEditor.Caret.Offset - 2), CancellationToken.None).WaitAndGetResult (CancellationToken.None); + var stack = editor.SyntaxHighlighting.GetScopeStackAsync (Math.Max (0, editor.CaretOffset - 2), CancellationToken.None).WaitAndGetResult (CancellationToken.None); foreach (var span in stack) { if (string.IsNullOrEmpty (span)) continue; @@ -72,7 +78,7 @@ namespace MonoDevelop.SourceEditor char openingBrace = openBrackets [braceIndex]; int count = 0; - foreach (char curCh in ExtensibleTextEditor.GetTextWithoutCommentsAndStrings(extEditor.Document, 0, extEditor.Document.Length)) { + foreach (char curCh in GetTextWithoutCommentsAndStrings(editor, 0, editor.Length)) { if (curCh == openingBrace) { count++; } else if (curCh == closingBrace) { @@ -98,6 +104,54 @@ namespace MonoDevelop.SourceEditor return false; } + + internal static IEnumerable<char> GetTextWithoutCommentsAndStrings (ITextSource doc, int start, int end) + { + bool isInString = false, isInChar = false; + bool isInLineComment = false, isInBlockComment = false; + int escaping = 0; + + for (int pos = start; pos < end; pos++) { + char ch = doc.GetCharAt (pos); + switch (ch) { + case '\r': + case '\n': + isInLineComment = false; + break; + case '/': + if (isInBlockComment) { + if (pos > 0 && doc.GetCharAt (pos - 1) == '*') + isInBlockComment = false; + } else if (!isInString && !isInChar && pos + 1 < doc.Length) { + char nextChar = doc.GetCharAt (pos + 1); + if (nextChar == '/') + isInLineComment = true; + if (!isInLineComment && nextChar == '*') + isInBlockComment = true; + } + break; + case '"': + if (!(isInChar || isInLineComment || isInBlockComment)) + if (!isInString || escaping != 1) + isInString = !isInString; + break; + case '\'': + if (!(isInString || isInLineComment || isInBlockComment)) + if (!isInChar || escaping != 1) + isInChar = !isInChar; + break; + case '\\': + if (escaping != 1) + escaping = 2; + break; + default: + if (!(isInString || isInChar || isInLineComment || isInBlockComment)) + yield return ch; + break; + } + escaping--; + } + } } } diff --git a/main/src/addins/MonoDevelop.SourceEditor2/MonoDevelop.SourceEditor/ExtensibleTextEditor.cs b/main/src/addins/MonoDevelop.SourceEditor2/MonoDevelop.SourceEditor/ExtensibleTextEditor.cs index 971ca476e5..ab817b74e2 100644 --- a/main/src/addins/MonoDevelop.SourceEditor2/MonoDevelop.SourceEditor/ExtensibleTextEditor.cs +++ b/main/src/addins/MonoDevelop.SourceEditor2/MonoDevelop.SourceEditor/ExtensibleTextEditor.cs @@ -287,54 +287,6 @@ namespace MonoDevelop.SourceEditor LoggingService.LogInternalError ("Error in text editor extension chain", ex); } - internal static IEnumerable<char> GetTextWithoutCommentsAndStrings (Mono.TextEditor.TextDocument doc, int start, int end) - { - bool isInString = false, isInChar = false; - bool isInLineComment = false, isInBlockComment = false; - int escaping = 0; - - for (int pos = start; pos < end; pos++) { - char ch = doc.GetCharAt (pos); - switch (ch) { - case '\r': - case '\n': - isInLineComment = false; - break; - case '/': - if (isInBlockComment) { - if (pos > 0 && doc.GetCharAt (pos - 1) == '*') - isInBlockComment = false; - } else if (!isInString && !isInChar && pos + 1 < doc.Length) { - char nextChar = doc.GetCharAt (pos + 1); - if (nextChar == '/') - isInLineComment = true; - if (!isInLineComment && nextChar == '*') - isInBlockComment = true; - } - break; - case '"': - if (!(isInChar || isInLineComment || isInBlockComment)) - if (!isInString || escaping != 1) - isInString = !isInString; - break; - case '\'': - if (!(isInString || isInLineComment || isInBlockComment)) - if (!isInChar || escaping != 1) - isInChar = !isInChar; - break; - case '\\': - if (escaping != 1) - escaping = 2; - break; - default : - if (!(isInString || isInChar || isInLineComment || isInBlockComment)) - yield return ch; - break; - } - escaping--; - } - } - protected internal override bool OnIMProcessedKeyPressEvent (Gdk.Key key, uint ch, Gdk.ModifierType state) { bool result = true; |