Exemplo n.º 1
0
void SonicPiScintilla::toggleComment() {
  beginUndoAction();

  int linenum, cursor;
  getCursorPosition(&linenum, &cursor);

  //select the whole line
  setSelection(linenum, 0, linenum, cursor+1);

  QString selection = selectedText();

  // make sure we don't comment empty lines
  if (selection.length() > 0) {
      // if it's already commented, uncomment
      if (selection[0] == '#') {
          selection.remove(0, 1);
          replaceSelectedText(selection);
          if (cursor > 0) {
              setCursorPosition(linenum, cursor - 1);
          } else {
              setCursorPosition(linenum, cursor);
          }
      } else {
          selection.prepend('#');
          replaceSelectedText(selection);
          setCursorPosition(linenum, cursor + 1);
      }
  }
  deselect();
  endUndoAction();
}
Exemplo n.º 2
0
void SonicPiScintilla::replaceBuffer(QString content, int line, int index, int first_line) {
  beginUndoAction();
  insert(" ");
  SendScintilla(QsciCommand::Delete);
  selectAll();
  replaceSelectedText(content);
  setCursorPosition(line, index);
  setFirstVisibleLine(first_line);
  endUndoAction();
}
Exemplo n.º 3
0
void SonicPiScintilla::moveLineOrSelection(int numLines) {
  beginUndoAction();

  int linenum, cursor, origLinenum, origCursor;
  getCursorPosition(&linenum, &cursor);
  origLinenum = linenum;
  origCursor = cursor;

  bool hadSelectedText = hasSelectedText();


  if(!hadSelectedText) {
    setSelection(linenum, 0, linenum + 1, 0);
  }

  int lineFrom, indexFrom, lineTo, indexTo, lineOffset;
  getSelection(&lineFrom, &indexFrom, &lineTo, &indexTo);
  lineOffset = lineTo - origLinenum;
  linenum = lineFrom;

  QString selection = selectedText();

  if(selection[selection.length()-1] != '\n') {
    selection = selection + "\n";
    lineTo += 1;
    lineOffset += 1;
    indexTo = 0;
    replaceSelectedText("");
    setCursorPosition(linenum, 0);
    SendScintilla(SCI_DELETEBACK);
  } else {
    replaceSelectedText("");
  }
  setCursorPosition(linenum, 0);

  moveLines(numLines);

  getCursorPosition(&linenum, &cursor);
  setCursorPosition(linenum, 0);
  insert(selection);

  setCursorPosition(linenum + lineOffset, origCursor);

  int diffLine = lineTo - lineFrom;
  int diffIndex = indexTo - indexFrom;

  setSelection(linenum + diffLine, diffIndex, linenum, 0);

  endUndoAction();
}
Exemplo n.º 4
0
void pEditor::convertTabs()
{
    // get original text
    QString originalText = text();
    // all modifications must believe as only one action
    beginUndoAction();
    // get indent width
    const int indentWidth = indentationWidth() != 0 ? indentationWidth() : tabWidth();
    // iterate each line
    for ( int i = 0; i < lines(); i++ )
    {
        // remember if last line was troncate
        static bool lastLineWasTroncate = false;
        // get current line indent width
        int lineIndent = indentation( i );
        // check if need troncate
        int t = lineIndent /indentWidth;
        int r = lineIndent %indentWidth;
        if ( r != 0 && r != indentWidth )
        {
            r += indentWidth -r;
            lineIndent = ( t *indentWidth) +r;
            lastLineWasTroncate = true;
        }
        else if ( lastLineWasTroncate && lineIndent != 0 )
        {
            lastLineWasTroncate = indentation( i +1 ) == lineIndent;
            lineIndent  += indentWidth;
        }
        // remove indentation
        setIndentation( i, 0 );
        // restore it with possible troncate indentation
        setIndentation( i, lineIndent );
    }
    // end global undo action
    endUndoAction();
    // compare original and newer text
    if ( originalText == text() )
    {
        // clear undo buffer
        SendScintilla( SCI_EMPTYUNDOBUFFER );
        // set unmodified
        setModified( false );
    }
}
Exemplo n.º 5
0
void SciDoc::stripTrailingSpaces() {
//	LOGGER;
	if ( int_->curEdit_ == NULL ) return;

	int line, col;
	getCursorPos(line, col);
	QString text = int_->curEdit_->text();
	QStringList lines = text.split(LineSeparatorRx);
	QRegExp rx("[ \t]+$");
	int i = 0;

	beginUndoAction();
	foreach (QString str, lines) {
		int pos = str.indexOf(rx);
		if ( pos >= 0 ) {
			int_->curEdit_->setSelection(i, 0, i, str.length());
			str.truncate(pos);
			replaceSelectedText(str);
		}
		++i;
	}
Exemplo n.º 6
0
void DocumentEditor::toggleComment(bool lineCommentPrefered_) {
	QsciLexer* l = lexer();
	if(l == 0)
		return;
	QString comment = l->commentLine();
	QStringList commentBlock = l->commentBlock();

	if(comment.isEmpty() && commentBlock.isEmpty()){
		qDebug() << "Toggle comment is not supported for " << l->language();
		return;
	}

	if (!hasSelectedText()) {
		//if line is empty, skip it
		int line = getCurrentLine();
		if (isLineEmpty(line))
			return;

		QString selText = text(line);
		selText.remove("\n");
		selText.remove("\r");
		QString selTextTrimmed = selText.trimmed();
		int pos_start = SendScintilla(SCI_POSITIONFROMLINE, line);
		int pos_end = SendScintilla(SCI_GETLINEENDPOSITION, line);

		// check for block comments on a line
		if(commentBlock.size() >= 2){
			QString blockStart = commentBlock.first();
			QString blockEnd = commentBlock.last();
			if (selTextTrimmed.startsWith(blockStart) && selTextTrimmed.endsWith(blockEnd)) {
				beginUndoAction();

				int idx1 = selText.indexOf(blockStart);
				selText.remove(idx1, blockEnd.size());
				int idx2 = selText.lastIndexOf(blockEnd);
				selText.remove(idx2, blockEnd.size());

				SendScintilla(SCI_SETTARGETSTART, pos_start);
				SendScintilla(SCI_SETTARGETEND, pos_end);
				SendScintilla(SCI_REPLACETARGET, -1, selText.toUtf8().data());

				endUndoAction();
				return;
			}
		}

		// check for single comments
		if (!comment.isEmpty()) {
			if (selTextTrimmed.startsWith(comment)) {
				// remove comment
				int idx = selText.indexOf(comment);
				selText = selText.remove(idx, comment.size());
			} else {
				// set comment
				selText = selText.prepend(comment);
			}

			SendScintilla(SCI_SETTARGETSTART, pos_start);
			SendScintilla(SCI_SETTARGETEND, pos_end);
			SendScintilla(SCI_REPLACETARGET, -1, selText.toUtf8().data());
			return;
		}

	}else{
		// comment out the selection
		QString selText = selectedText();
		QString selTextTrimmed = selText.trimmed();
		if (selTextTrimmed.isEmpty())
			return;

		int lineFrom, lineTo, indexFrom, indexTo;
		getSelection(&lineFrom, &indexFrom, &lineTo, &indexTo);

		int pos_start = positionFromLineIndex(lineFrom, indexFrom);
		int pos_end = positionFromLineIndex(lineTo, indexTo);

		// check if it is double commented block - to do before single check!
		if(commentBlock.size() >= 2){
			QString blockStart = commentBlock.first();
			QString blockEnd = commentBlock.last();
			// comment exists? remove?
			if (selTextTrimmed.startsWith(blockStart) && selTextTrimmed.endsWith(blockEnd)) {
				beginUndoAction();

				int idx1 = selText.indexOf(blockStart);
				selText.remove(idx1, blockStart.size());
				int idx2 = selText.lastIndexOf(blockEnd);
				selText.remove(idx2, blockEnd.size());

				SendScintilla(SCI_TARGETFROMSELECTION);
				SendScintilla(SCI_REPLACETARGET, -1, selText.toUtf8().data());
				SendScintilla(SCI_SETSEL, SendScintilla(SCI_GETTARGETSTART), SendScintilla(SCI_GETTARGETEND));

				endUndoAction();
				return;
			}
		}

		// check if this block can be single commented
		if (!comment.isEmpty() && lineCommentPrefered_) {
			bool empty_start = false, empty_end = false;

			if (indexFrom == 0)
				empty_start = true;
			else
				empty_start = getTextRange(positionFromLineIndex(lineFrom, 0), pos_start).trimmed().isEmpty();

			if (indexTo == 0)
				empty_end = true;
			else
				empty_end = getTextRange(pos_end, positionFromLineIndex(lineTo+1, 0)).trimmed().isEmpty();

			if (empty_start && empty_end) {
				beginUndoAction();

				// corrections
				if (indexTo == 0)
					lineTo--;
				if (isLineEmpty(lineFrom)) {
					lineFrom++; indexFrom = 0;
				}
				// a workaround: move cursor to the next line to replace EOL as well
				setSelection(lineFrom, 0, lineTo+1, 0);

				QStringList sl;
				for (int i = lineFrom; i <= lineTo; i++)
					sl += text(i);

				bool comm = false;
				for (int i = 0; i < sl.count(); i++)
					if (!sl.at(i).trimmed().startsWith(comment)) {
						comm = true;
						break;
					}

				for (int i = 0; i < sl.count(); i++) {
					if (comm)
						sl[i] = sl[i].prepend(comment);
					else {
						int idx = sl.at(i).indexOf(comment);
						sl[i] = sl[i].remove(idx, comment.size());
					}
				}

				SendScintilla(SCI_TARGETFROMSELECTION);
				SendScintilla(SCI_REPLACETARGET, -1, sl.join("").toUtf8().data());
				SendScintilla(SCI_SETSEL, SendScintilla(SCI_GETTARGETSTART), SendScintilla(SCI_GETTARGETEND));

				endUndoAction();
				return;
			}
		}

		// else, set double comment
		if(commentBlock.size() >= 2){
			QString blockStart = commentBlock.first();
			QString blockEnd = commentBlock.last();
			beginUndoAction();

			// last is first
			SendScintilla(SCI_INSERTTEXT, pos_end, blockEnd.toUtf8().data());
			SendScintilla(SCI_INSERTTEXT, pos_start, blockStart.toUtf8().data());

			// select everything
			if(lineFrom == lineTo)
				setSelection(lineFrom, indexFrom, lineTo, indexTo + blockStart.size() + blockEnd.size());
			else
				setSelection(lineFrom, indexFrom, lineTo, indexTo + blockEnd.size());

			endUndoAction();
			return;
		}
	}
}