/// <summary>
    /// This function sets the indentation level in a range of lines.
    /// </summary>
    /// <param name="textArea">The text area.</param>
    /// <param name="begin">The begin.</param>
    /// <param name="end">The end.</param>
    public override void IndentLines(TextArea textArea, int begin, int end)
    {
      if (textArea.Document.TextEditorProperties.IndentStyle != IndentStyle.Smart)
      {
        base.IndentLines(textArea, begin, end);
        return;
      }
      int cursorPos = textArea.Caret.Position.Y;
      int oldIndentLength = 0;

      if (cursorPos >= begin && cursorPos <= end)
        oldIndentLength = GetIndentation(textArea, cursorPos).Length;

      IndentationSettings set = new IndentationSettings();
      set.IndentString = Tab.GetIndentationString(textArea.Document);
      CSharpIndentationReformatter r = new CSharpIndentationReformatter();
      DocumentAccessor acc = new DocumentAccessor(textArea.Document, begin, end);
      r.Reformat(acc, set);

      if (cursorPos >= begin && cursorPos <= end)
      {
        int newIndentLength = GetIndentation(textArea, cursorPos).Length;
        if (oldIndentLength != newIndentLength)
        {
          // fix cursor position if indentation was changed
          int newX = textArea.Caret.Position.X - oldIndentLength + newIndentLength;
          textArea.Caret.Position = new TextLocation(Math.Max(newX, 0), cursorPos);
        }
      }
    }
		public override ICompletionData[] GenerateCompletionData(string fileName, TextArea textArea, char charTyped) {
			// This class provides the data for the Code-Completion-Window.
			// Some random variables and methods are returned as completion data.
			List<ICompletionData> completionData = new List<ICompletionData> {
				// Add some random variables
				new DefaultCompletionData("a", "A local variable", 1),
				new DefaultCompletionData("b", "A local variable", 1),
				new DefaultCompletionData("variableX", "A local variable", 1),
				new DefaultCompletionData("variableY", "A local variable", 1),
				// Add some random methods
				new DefaultCompletionData("MethodA", "A simple method.", 2),
				new DefaultCompletionData("MethodB", "A simple method.", 2),
				new DefaultCompletionData("MethodC", "A simple method.", 2)
			};
			// Add some snippets (text templates).
			List<Snippet> snippets = new List<Snippet> {
		        new Snippet("for", "for (|;;)\n{\n}", "for loop"),
		        new Snippet("if", "if (|)\n{\n}", "if statement"),
		        new Snippet("ifel", "if (|)\n{\n}\nelse\n{\n}", "if-else statement"),
		        new Snippet("while", "while (|)\n{\n}", "while loop"),
		        new Snippet("dr", "Visit http://www.digitalrune.com/", "The URL of DigitalRune Software.")
			};
			// Add the snippets to the completion data
			foreach (Snippet snippet in snippets) {
				completionData.Add(new SnippetCompletionData(snippet, 0));
			}
			return completionData.ToArray();
		}
    /// <summary>
    /// Define CSharp specific smart indenting for a line :)
    /// </summary>
    protected override int SmartIndentLine(TextArea textArea, int lineNr)
    {
      if (lineNr <= 0)
        return AutoIndentLine(textArea, lineNr);

      string oldText = textArea.Document.GetText(textArea.Document.GetLineSegment(lineNr));

      DocumentAccessor acc = new DocumentAccessor(textArea.Document, lineNr, lineNr);

      IndentationSettings set = new IndentationSettings();
      set.IndentString = Tab.GetIndentationString(textArea.Document);
      set.LeaveEmptyLines = false;
      CSharpIndentationReformatter r = new CSharpIndentationReformatter();

      r.Reformat(acc, set);

      string t = acc.Text;
      if (t.Length == 0)
      {
        // use AutoIndentation for new lines in comments / verbatim strings.
        return AutoIndentLine(textArea, lineNr);
      }
      else
      {
        int newIndentLength = t.Length - t.TrimStart().Length;
        int oldIndentLength = oldText.Length - oldText.TrimStart().Length;
        if (oldIndentLength != newIndentLength && lineNr == textArea.Caret.Position.Y)
        {
          // fix cursor position if indentation was changed
          int newX = textArea.Caret.Position.X - oldIndentLength + newIndentLength;
          textArea.Caret.Position = new TextLocation(Math.Max(newX, 0), lineNr);
        }
        return newIndentLength;
      }
    }
    /// <summary>
    /// Attaches the specified text area.
    /// </summary>
    /// <param name="textArea">The text area.</param>
    public void Attach(TextArea textArea)
    {
      _textArea = textArea;
      textArea.AllowDrop = true;

      textArea.DragEnter += new SafeDragEventHandler(OnDragEnter).OnDragEvent;
      textArea.DragDrop += new SafeDragEventHandler(OnDragDrop).OnDragEvent;
      textArea.DragOver += new SafeDragEventHandler(OnDragOver).OnDragEvent;
    }
        public override ICompletionData[] GenerateCompletionData(string fileName, TextArea textArea, char charTyped)
        {
            foreach (var item in _intellisense._icons)
            {
                _images.Images.Add(item);
            }

            return _intellisense._data.ToArray();
        }
Beispiel #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HRuler"/> class.
        /// </summary>
        /// <param name="textArea">The text area.</param>
        public HRuler(TextArea textArea)
        {
            _textArea = textArea;
              SetStyle(ControlStyles.AllPaintingInWmPaint, true);
              SetStyle(ControlStyles.UserPaint, true);
              SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
              SetStyle(ControlStyles.ResizeRedraw, true);
              SetStyle(ControlStyles.FixedHeight, true);

              HighlightColor lineNumberPainterColor = textArea.Document.HighlightingStrategy.GetColorFor("LineNumbers");
              Font lineNumberFont = lineNumberPainterColor.GetFont(textArea.TextEditorProperties.FontContainer);
              Font = new Font(lineNumberFont.FontFamily, lineNumberFont.Size * 3.0f / 4.0f, FontStyle.Regular);
              Size textSize = TextRenderer.MeasureText("0", Font);
              Height = textSize.Height * 5 / 4;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="TextAreaControl"/> class.
        /// </summary>
        /// <param name="motherTextEditorControl">The mother text editor control.</param>
        public TextAreaControl(TextEditorControl motherTextEditorControl)
        {
            _motherTextEditorControl = motherTextEditorControl;
              _textArea = new TextArea(motherTextEditorControl, this);
              Controls.Add(_textArea);

              _vScrollBar.ValueChanged += VScrollBarValueChanged;
              _hScrollBar.ValueChanged += HScrollBarValueChanged;
              SetScrollBars();

              ResizeRedraw = true;

              Document.TextContentChanged += DocumentTextContentChanged;
              Document.DocumentChanged += AdjustScrollBarsOnDocumentChange;
              Document.UpdateCommited += DocumentUpdateCommitted;
        }
Beispiel #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Caret"/> class.
 /// </summary>
 /// <param name="textArea">The text area.</param>
 public Caret(TextArea textArea)
 {
     _textArea = textArea;
       textArea.GotFocus += GotFocus;
       textArea.LostFocus += LostFocus;
 }
 static bool IsInsideDocumentationComment(TextArea textArea, ISegment curLine, int cursorOffset)
 {
   for (int i = curLine.Offset; i < cursorOffset; ++i)
   {
     char ch = textArea.Document.GetCharAt(i);
     if (ch == '"')
     {
       // parsing strings correctly is too complicated (see above),
       // but I don't now any case where a doc comment is after a string...
       return false;
     }
     if (ch == '/' && i + 2 < cursorOffset && textArea.Document.GetCharAt(i + 1) == '/' && textArea.Document.GetCharAt(i + 2) == '/')
     {
       return true;
     }
   }
   return false;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="TextAreaClipboardHandler"/> class.
 /// </summary>
 /// <param name="textArea">The text area.</param>
 public TextAreaClipboardHandler(TextArea textArea)
 {
   _textArea = textArea;
   textArea.SelectionManager.SelectionChanged += DocumentSelectionChanged;
 }
    static bool IsInsideStringOrComment(TextArea textArea, ISegment curLine, int cursorOffset)
    {
      // scan cur line if it is inside a string or single line comment (//)
      bool insideString = false;
      char stringstart = ' ';
      bool verbatim = false; // true if the current string is verbatim (@-string)
      char c = ' ';

      for (int i = curLine.Offset; i < cursorOffset; ++i)
      {
        char lastchar = c;
        c = textArea.Document.GetCharAt(i);
        if (insideString)
        {
          if (c == stringstart)
          {
            if (verbatim && i + 1 < cursorOffset && textArea.Document.GetCharAt(i + 1) == '"')
            {
              ++i; // skip escaped character
            }
            else
            {
              insideString = false;
            }
          }
          else if (c == '\\' && !verbatim)
          {
            ++i; // skip escaped character
          }
        }
        else if (c == '/' && i + 1 < cursorOffset && textArea.Document.GetCharAt(i + 1) == '/')
        {
          return true;
        }
        else if (c == '"' || c == '\'')
        {
          stringstart = c;
          insideString = true;
          verbatim = (c == '"') && (lastchar == '@');
        }
      }

      return insideString;
    }
 /// <summary>
 /// Initializes a new instance of the <see cref="IconMargin"/> class.
 /// </summary>
 /// <param name="textArea">The text area.</param>
 public IconMargin(TextArea textArea)
   : base(textArea)
 {
 }
 /// <summary>
 /// Gets the word before caret.
 /// </summary>
 /// <returns>The word.</returns>
 /// <remarks>
 /// The 
 /// </remarks>
 public static string GetWordBeforeCaret(TextArea textArea)
 {
   int start = FindPrevWordStart(textArea.Document, textArea.Caret.Offset);
   return textArea.Document.GetText(start, textArea.Caret.Offset - start);
 }
Beispiel #14
0
 /// <summary>
 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 /// </summary>
 public void Dispose()
 {
     DisposeCaret();
       _textArea.GotFocus -= GotFocus;
       _textArea.LostFocus -= LostFocus;
       _textArea = null;
 }
 /// <summary>
 /// Deletes the word before caret.
 /// </summary>
 /// <returns>The new offset of the caret.</returns>
 public static int DeleteWordBeforeCaret(TextArea textArea)
 {
   int start = FindPrevWordStart(textArea.Document, textArea.Caret.Offset);
   textArea.Document.Remove(start, textArea.Caret.Offset - start);
   return start;
 }
 /// <summary>
 /// Formats a specific line after <c>ch</c> is pressed.
 /// </summary>
 /// <param name="textArea">The text area.</param>
 /// <param name="lineNr">The line number.</param>
 /// <param name="cursorOffset">The cursor offset.</param>
 /// <param name="ch">The ch.</param>
 /// <returns>
 /// The caret delta position the caret will be moved this number
 /// of bytes (e.g. the number of bytes inserted before the caret, or
 /// removed, if this number is negative)
 /// </returns>
 public override void FormatLine(TextArea textArea, int lineNr, int cursorOffset, char ch) // used for comment tag formater/inserter
 {
   textArea.Document.UndoStack.StartUndoGroup();
   FormatLineInternal(textArea, lineNr, cursorOffset, ch);
   textArea.Document.UndoStack.EndUndoGroup();
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="AbstractMargin"/> class.
 /// </summary>
 /// <param name="textArea">The text area.</param>
 protected AbstractMargin(TextArea textArea)
 {
   _textArea = textArea;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="TextAreaMouseHandler"/> class.
 /// </summary>
 /// <param name="textArea">The text area.</param>
 public TextAreaMouseHandler(TextArea textArea)
 {
   _textArea = textArea;
 }
    private void FormatLineInternal(TextArea textArea, int lineNr, int cursorOffset, char ch) // used for comment tag formatter/inserter
    {
      LineSegment curLine = textArea.Document.GetLineSegment(lineNr);
      LineSegment lineAbove = lineNr > 0 ? textArea.Document.GetLineSegment(lineNr - 1) : null;
      string terminator = textArea.TextEditorProperties.LineTerminator;

      // local string for curLine segment
      string curLineText;
      if (ch == '/')
      {
        curLineText = textArea.Document.GetText(curLine);
        string lineAboveText = lineAbove == null ? "" : textArea.Document.GetText(lineAbove);
        if (curLineText != null && curLineText.EndsWith("///") && (lineAboveText == null || !lineAboveText.Trim().StartsWith("///")))
        {

          // TODO: Automatic insertion of XML comments disabled. Needs to be ported from SharpDevelop.
          /* 
                    string indentation = base.GetIndentation(textArea, lineNr);
                    object member = GetMemberAfter(textArea, lineNr);
                    if (member != null)
                    {
                      StringBuilder sb = new StringBuilder();
                      sb.Append(" <summary>");
                      sb.Append(terminator);
                      sb.Append(indentation);
                      sb.Append("/// ");
                      sb.Append(terminator);
                      sb.Append(indentation);
                      sb.Append("/// </summary>");

                      if (member is IMethod)
                      {
                        IMethod method = (IMethod) member;
                        if (method.Parameters != null && method.Parameters.Count > 0)
                        {
                          for (int i = 0; i < method.Parameters.Count; ++i)
                          {
                            sb.Append(terminator);
                            sb.Append(indentation);
                            sb.Append("/// <param name=\"");
                            sb.Append(method.Parameters[i].Name);
                            sb.Append("\"></param>");
                          }
                        }
                        if (method.ReturnType != null && !method.IsConstructor && method.ReturnType.FullyQualifiedName != "System.Void")
                        {
                          sb.Append(terminator);
                          sb.Append(indentation);
                          sb.Append("/// <returns></returns>");
                        }
                      }
                      textArea.Document.Insert(cursorOffset, sb.ToString());

                      textArea.Refresh();
                      textArea.Caret.Position = textArea.Document.OffsetToPosition(cursorOffset + indentation.Length + "/// ".Length + " <summary>".Length + terminator.Length);
                    }
           */
        }
        return;
      }

      if (ch != '\n' && ch != '>')
      {
        if (IsInsideStringOrComment(textArea, curLine, cursorOffset))
        {
          return;
        }
      }
      switch (ch)
      {
        case '>':
          if (IsInsideDocumentationComment(textArea, curLine, cursorOffset))
          {
            curLineText = textArea.Document.GetText(curLine);
            int column = textArea.Caret.Offset - curLine.Offset;
            int index = Math.Min(column - 1, curLineText.Length - 1);

            while (index >= 0 && curLineText[index] != '<')
            {
              --index;
              if (curLineText[index] == '/')
                return; // the tag was an end tag or already
            }

            if (index > 0)
            {
              StringBuilder commentBuilder = new StringBuilder("");
              for (int i = index; i < curLineText.Length && i < column && !Char.IsWhiteSpace(curLineText[i]); ++i)
              {
                commentBuilder.Append(curLineText[i]);
              }
              string tag = commentBuilder.ToString().Trim();
              if (!tag.EndsWith(">"))
              {
                tag += ">";
              }
              if (!tag.StartsWith("/"))
              {
                textArea.Document.Insert(textArea.Caret.Offset, "</" + tag.Substring(1));
              }
            }
          }
          break;
        case ':':
        case ')':
        case ']':
        case '}':
        case '{':
          if (textArea.Document.TextEditorProperties.IndentStyle == IndentStyle.Smart)
          {
            textArea.Document.FormattingStrategy.IndentLine(textArea, lineNr);
          }
          break;
        case '\n':
          string lineAboveText = lineAbove == null ? "" : textArea.Document.GetText(lineAbove);
          //// curLine might have some text which should be added to indentation
          curLineText = "";
          if (curLine.Length > 0)
          {
            curLineText = textArea.Document.GetText(curLine);
          }

          LineSegment nextLine = lineNr + 1 < textArea.Document.TotalNumberOfLines ? textArea.Document.GetLineSegment(lineNr + 1) : null;
          string nextLineText = lineNr + 1 < textArea.Document.TotalNumberOfLines ? textArea.Document.GetText(nextLine) : "";

          int addCursorOffset = 0;

          if (lineAboveText.Trim().StartsWith("#region") && NeedEndregion(textArea.Document))
          {
            textArea.Document.Insert(curLine.Offset, "#endregion");
            textArea.Caret.Column = IndentLine(textArea, lineNr);
            return;
          }

          if (lineAbove != null && lineAbove.HighlightSpanStack != null && !lineAbove.HighlightSpanStack.IsEmpty)
          {
            if (!lineAbove.HighlightSpanStack.Peek().StopEOL)
            {
              // case for /* style comments
              int index = lineAboveText.IndexOf("/*");
              if (index > 0)
              {
                StringBuilder indentation = new StringBuilder(GetIndentation(textArea, lineNr - 1));
                for (int i = indentation.Length; i < index; ++i)
                {
                  indentation.Append(' ');
                }
                // adding curline text
                textArea.Document.Replace(curLine.Offset, curLine.Length, indentation + " * " + curLineText);
                textArea.Caret.Column = indentation.Length + 3 + curLineText.Length;
                return;
              }

              index = lineAboveText.IndexOf("*");
              if (index > 0)
              {
                StringBuilder indentation = new StringBuilder(GetIndentation(textArea, lineNr - 1));
                for (int i = indentation.Length; i < index; ++i)
                {
                  indentation.Append(' ');
                }
                // adding curline if present
                textArea.Document.Replace(curLine.Offset, curLine.Length, indentation + "* " + curLineText);
                textArea.Caret.Column = indentation.Length + 2 + curLineText.Length;
                return;
              }
            }
            else
            { // don't handle // lines, because they're only one lined comments
              int indexAbove = lineAboveText.IndexOf("///");
              int indexNext = nextLineText.IndexOf("///");
              if (indexAbove > 0 && (indexNext != -1 || indexAbove + 4 < lineAbove.Length))
              {
                StringBuilder indentation = new StringBuilder(GetIndentation(textArea, lineNr - 1));
                for (int i = indentation.Length; i < indexAbove; ++i)
                {
                  indentation.Append(' ');
                }
                // adding curline text if present
                textArea.Document.Replace(curLine.Offset, curLine.Length, indentation + "/// " + curLineText);
                textArea.Caret.Column = indentation.Length + 4;
                return;
              }

              if (IsInNonVerbatimString(lineAboveText, curLineText))
              {
                textArea.Document.Insert(lineAbove.Offset + lineAbove.Length,
                                         "\" +");
                curLine = textArea.Document.GetLineSegment(lineNr);
                textArea.Document.Insert(curLine.Offset, "\"");
                addCursorOffset = 1;
              }
            }
          }
          int result = IndentLine(textArea, lineNr) + addCursorOffset;
          if (textArea.TextEditorProperties.AutoInsertCurlyBracket)
          {
            string oldLineText = TextHelper.GetLineAsString(textArea.Document, lineNr - 1);
            if (oldLineText.EndsWith("{"))
            {
              if (NeedCurlyBracket(textArea.Document.TextContent))
              {
                textArea.Document.Insert(curLine.Offset + curLine.Length, terminator + "}");
                IndentLine(textArea, lineNr + 1);
              }
            }
          }
          textArea.Caret.Column = result;
          return;
      }
    }
 /// <summary>
 /// Initializes a new instance of the <see cref="FoldMargin"/> class.
 /// </summary>
 /// <param name="TextArea">The text area.</param>
 public FoldMargin(TextArea TextArea)
   : base(TextArea)
 {
 }
Beispiel #21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="LineNumberMargin"/> class.
 /// </summary>
 /// <param name="TextArea">The text area.</param>
 public LineNumberMargin(TextArea TextArea)
     : base(TextArea)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="TextView"/> class.
 /// </summary>
 /// <param name="textArea">The text area.</param>
 public TextView(TextArea textArea)
   : base(textArea)
 {
   base.Cursor = Cursors.IBeam;
   OptionsChanged();
 }