Ejemplo n.º 1
0
 private void TextArea_TextEntered(object sender, TextCompositionEventArgs e)
 {
     if (Program.OptionsObject.Editor_ReformatLineAfterSemicolon)
     {
         if (e.Text == ";")
         {
             if (editor.CaretOffset >= 0)
             {
                 var    line = editor.Document.GetLineByOffset(editor.CaretOffset);
                 var    leadingIndentation = editor.Document.GetText(TextUtilities.GetLeadingWhitespace(editor.Document, line));
                 string newLineStr         = leadingIndentation + SPSyntaxTidy.TidyUp(editor.Document.GetText(line)).Trim();
                 editor.Document.Replace(line, newLineStr);
             }
         }
     }
     if (e.Text == "}") //force indentate line so we can evaluate the indentation
     {
         editor.TextArea.IndentationStrategy.IndentLine(editor.Document, editor.Document.GetLineByOffset(editor.CaretOffset));
         foldingStrategy.UpdateFoldings(foldingManager, editor.Document);
     }
     else if (e.Text == "{")
     {
         foldingStrategy.UpdateFoldings(foldingManager, editor.Document);
     }
 }
Ejemplo n.º 2
0
        private void Command_TidyCode(bool All)
        {
            var editors = All ? GetAllEditorElements() : new[] { GetCurrentEditorElement() };

            foreach (var ee in editors)
            {
                ee?.editor.Document.BeginUpdate();
                string source = ee?.editor.Text;
                ee?.editor.Document.Replace(0, source.Length, SPSyntaxTidy.TidyUp(source));
                ee?.editor.Document.EndUpdate();
            }
        }
Ejemplo n.º 3
0
 private void Command_TidyCode(bool All)
 {
     EditorElement[] editors;
     if (All)
     {
         editors = GetAllEditorElements();
     }
     else
     {
         editors = new EditorElement[] { GetCurrentEditorElement() };
     }
     for (int i = 0; i < editors.Length; ++i)
     {
         EditorElement ee = editors[i];
         if (ee != null)
         {
             ee.editor.Document.BeginUpdate();
             string source = ee.editor.Text;
             ee.editor.Document.Replace(0, source.Length, SPSyntaxTidy.TidyUp(source));
             ee.editor.Document.EndUpdate();
         }
     }
 }
Ejemplo n.º 4
0
 private void TextArea_TextEntered(object sender, TextCompositionEventArgs e)
 {
     if (Program.OptionsObject.Editor_ReformatLineAfterSemicolon)
     {
         if (e.Text == ";")
         {
             if (editor.CaretOffset >= 0)
             {
                 var    line = editor.Document.GetLineByOffset(editor.CaretOffset);
                 var    leadingIndentation = editor.Document.GetText(TextUtilities.GetLeadingWhitespace(editor.Document, line));
                 string newLineStr         = leadingIndentation + SPSyntaxTidy.TidyUp(editor.Document.GetText(line)).Trim();
                 editor.Document.Replace(line, newLineStr);
             }
         }
     }
     if (e.Text == "}") //force indentate line so we can evaluate the indentation
     {
         editor.TextArea.IndentationStrategy.IndentLine(editor.Document, editor.Document.GetLineByOffset(editor.CaretOffset));
         foldingStrategy.UpdateFoldings(foldingManager, editor.Document);
     }
     else if (e.Text == "{")
     {
         if (Program.OptionsObject.Editor_AutoCloseBrackets)
         {
             editor.Document.Insert(editor.CaretOffset, "}");
             editor.CaretOffset -= 1;
         }
         foldingStrategy.UpdateFoldings(foldingManager, editor.Document);
     }
     else if (Program.OptionsObject.Editor_AutoCloseBrackets)
     {
         if (e.Text == "(")
         {
             editor.Document.Insert(editor.CaretOffset, ")");
             editor.CaretOffset -= 1;
         }
         else if (e.Text == "[")
         {
             editor.Document.Insert(editor.CaretOffset, "]");
             editor.CaretOffset -= 1;
         }
     }
     if (Program.OptionsObject.Editor_AutoCloseStringChars)
     {
         if (e.Text == "\"")
         {
             var    line     = editor.Document.GetLineByOffset(editor.CaretOffset);
             string lineText = editor.Document.GetText(line.Offset, editor.CaretOffset - line.Offset);
             if (lineText.Length > 0)
             {
                 if (lineText[Math.Max(lineText.Length - 2, 0)] != '\\')
                 {
                     editor.Document.Insert(editor.CaretOffset, "\"");
                     editor.CaretOffset -= 1;
                 }
             }
         }
         else if (e.Text == "'")
         {
             var    line     = editor.Document.GetLineByOffset(editor.CaretOffset);
             string lineText = editor.Document.GetText(line.Offset, editor.CaretOffset - line.Offset);
             if (lineText.Length > 0)
             {
                 if (lineText[Math.Max(lineText.Length - 2, 0)] != '\\')
                 {
                     editor.Document.Insert(editor.CaretOffset, "'");
                     editor.CaretOffset -= 1;
                 }
             }
         }
     }
 }
Ejemplo n.º 5
0
        private void Command_TidyCode(bool All)
        {
            var editors = All ? GetAllEditorElements() : new[] { GetCurrentEditorElement() };

            if (editors == null)
            {
                return;
            }
            foreach (var ee in editors)
            {
                if (ee != null)
                {
                    int currentCaret = ee.editor.TextArea.Caret.Offset, numOfSpacesOrTabsBefore = 0;
                    var line       = ee.editor.Document.GetLineByOffset(currentCaret);
                    var lineNumber = line.LineNumber;
                    // 0 - start | any other - middle | -1 - EOS
                    var curserLinePos = currentCaret == line.Offset ? 0 : currentCaret == line.EndOffset ? -1 : currentCaret - line.Offset;

                    if (curserLinePos > 0)
                    {
                        numOfSpacesOrTabsBefore = ee.editor.Document.GetText(line).Count(c => c == ' ' || c == '\t');
                    }

#if DEBUG
                    Debug.WriteLine($"Curser offset before format: {currentCaret}");

                    // Where is out curser?
                    if (currentCaret == line.Offset)
                    {
                        Debug.WriteLine("Curser is at the start of the line");
                    }
                    else if (currentCaret == line.EndOffset)
                    {
                        Debug.WriteLine("Curser is at the end of the line");
                    }
                    else
                    {
                        Debug.WriteLine("Curser is somewhere in the middle of the line");
                    }
#endif
                    // Formatting Start //
                    ee.editor.Document.BeginUpdate();
                    var source = ee.editor.Text;
                    ee.editor.Document.Replace(0, source.Length, SPSyntaxTidy.TidyUp(source));
                    ee.editor.Document.EndUpdate();
                    // Formatting End //

                    line = ee.editor.Document.GetLineByNumber(lineNumber);
                    var newCaretPos = line.Offset;
                    if (curserLinePos == -1)
                    {
                        newCaretPos += line.Length;
                    }
                    else if (curserLinePos != 0)
                    {
                        var numOfSpacesOrTabsAfter = ee.editor.Document.GetText(line).Count(c => c == ' ' || c == '\t');
                        newCaretPos += curserLinePos + (numOfSpacesOrTabsAfter - numOfSpacesOrTabsBefore);
#if DEBUG
                        Debug.WriteLine($"Curser offset after format: {newCaretPos}");
#endif
                    }
                    ee.editor.TextArea.Caret.Offset = newCaretPos;
                }
            }
        }