diff options
author | Zufu Liu <unknown> | 2023-01-12 22:19:17 +1100 |
---|---|---|
committer | Zufu Liu <unknown> | 2023-01-12 22:19:17 +1100 |
commit | 28ad37395deee65fb4a20a6bc59453d06505b0e7 (patch) | |
tree | c837867c719a009480c23cc2216292356c3756a3 | |
parent | 7ac090022ebef882b2b403174b06a3edf391e705 (diff) | |
download | scintilla-mirror-28ad37395deee65fb4a20a6bc59453d06505b0e7.tar.gz |
Feature [feature-requests:#1474] Simplify with InsertString(string_view).
-rw-r--r-- | src/Document.cxx | 5 | ||||
-rw-r--r-- | src/Editor.cxx | 23 | ||||
-rw-r--r-- | test/simpleTests.py | 11 |
3 files changed, 23 insertions, 16 deletions
diff --git a/src/Document.cxx b/src/Document.cxx index 9d25f0b08..03a3d4326 100644 --- a/src/Document.cxx +++ b/src/Document.cxx @@ -1570,13 +1570,12 @@ Sci::Position Document::SetLineIndentation(Sci::Line line, Sci::Position indent) if (indent < 0) indent = 0; if (indent != indentOfLine) { - std::string linebuf = CreateIndentation(indent, tabInChars, !useTabs); + const std::string linebuf = CreateIndentation(indent, tabInChars, !useTabs); const Sci::Position thisLineStart = LineStart(line); const Sci::Position indentPos = GetLineIndentPosition(line); UndoGroup ug(this); DeleteChars(thisLineStart, indentPos - thisLineStart); - return thisLineStart + InsertString(thisLineStart, linebuf.c_str(), - linebuf.length()); + return thisLineStart + InsertString(thisLineStart, linebuf); } else { return GetLineIndentPosition(line); } diff --git a/src/Editor.cxx b/src/Editor.cxx index 8aab313f3..082385391 100644 --- a/src/Editor.cxx +++ b/src/Editor.cxx @@ -1913,8 +1913,8 @@ Sci::Position Editor::RealizeVirtualSpace(Sci::Position position, Sci::Position if (indent == position) { return pdoc->SetLineIndentation(line, pdoc->GetLineIndentation(line) + virtualSpace); } else { - std::string spaceText(virtualSpace, ' '); - const Sci::Position lengthInserted = pdoc->InsertString(position, spaceText.c_str(), virtualSpace); + const std::string spaceText(virtualSpace, ' '); + const Sci::Position lengthInserted = pdoc->InsertString(position, spaceText); position += lengthInserted; } } @@ -1981,7 +1981,7 @@ void Editor::InsertCharacter(std::string_view sv, CharacterSource charSource) { } } positionInsert = RealizeVirtualSpace(positionInsert, currentSel->caret.VirtualSpace()); - const Sci::Position lengthInserted = pdoc->InsertString(positionInsert, sv.data(), sv.length()); + const Sci::Position lengthInserted = pdoc->InsertString(positionInsert, sv); if (lengthInserted > 0) { currentSel->caret.SetPosition(positionInsert + lengthInserted); currentSel->anchor.SetPosition(positionInsert + lengthInserted); @@ -3013,10 +3013,8 @@ void Editor::LineTranspose() { pdoc->DeleteChars(startPrevious, linePrevious.length()); startCurrent -= linePrevious.length(); - startCurrent += pdoc->InsertString(startPrevious, lineCurrent.c_str(), - lineCurrent.length()); - pdoc->InsertString(startCurrent, linePrevious.c_str(), - linePrevious.length()); + startCurrent += pdoc->InsertString(startPrevious, lineCurrent); + pdoc->InsertString(startCurrent, linePrevious); // Move caret to start of current line MovePositionTo(SelectionPosition(startCurrent)); } @@ -3043,8 +3041,8 @@ void Editor::LineReverse() { pdoc->DeleteChars(lineStart2, lineLen2); pdoc->DeleteChars(lineStart1, lineLen1); lineStart2 -= lineLen1; - pdoc->InsertString(lineStart2, line1.c_str(), lineLen1); - pdoc->InsertString(lineStart1, line2.c_str(), lineLen2); + pdoc->InsertString(lineStart2, line1); + pdoc->InsertString(lineStart1, line2); } // Wholly select all affected lines sel.RangeMain() = SelectionRange(pdoc->LineStart(lineStart), @@ -4015,8 +4013,7 @@ void Editor::Indent(bool forwards) { if (numSpaces < 1) numSpaces = pdoc->tabInChars; const std::string spaceText(numSpaces, ' '); - const Sci::Position lengthInserted = pdoc->InsertString(caretPosition, spaceText.c_str(), - spaceText.length()); + const Sci::Position lengthInserted = pdoc->InsertString(caretPosition, spaceText); sel.Range(r) = SelectionRange(caretPosition + lengthInserted); } } @@ -4400,7 +4397,7 @@ void Editor::DropAt(SelectionPosition position, const char *value, size_t length position = MovePositionOutsideChar(position, sel.MainCaret() - position.Position()); position = RealizeVirtualSpace(position); const Sci::Position lengthInserted = pdoc->InsertString( - position.Position(), convertedText.c_str(), convertedText.length()); + position.Position(), convertedText); if (lengthInserted > 0) { SelectionPosition posAfterInsertion = position; posAfterInsertion.Add(lengthInserted); @@ -5763,7 +5760,7 @@ void Editor::AddStyledText(const char *buffer, Sci::Position appendLength) { for (i = 0; i < textLength; i++) { text[i] = buffer[i*2]; } - const Sci::Position lengthInserted = pdoc->InsertString(CurrentPosition(), text.c_str(), textLength); + const Sci::Position lengthInserted = pdoc->InsertString(CurrentPosition(), text); for (i = 0; i < textLength; i++) { text[i] = buffer[i*2+1]; } diff --git a/test/simpleTests.py b/test/simpleTests.py index 289359475..9f1b97c98 100644 --- a/test/simpleTests.py +++ b/test/simpleTests.py @@ -607,6 +607,17 @@ class TestSimple(unittest.TestCase): self.ed.LineTranspose() self.assertEquals(self.ed.Contents(), b"b2\na1\nc3") + def testReverseLines(self): + self.ed.SetContents(b"a1\nb2\nc3") + self.ed.SetSel(0, 8) + self.ed.LineReverse() + self.assertEquals(self.ed.Contents(), b"c3\nb2\na1") + + self.ed.SetContents(b"a1\nb2\nc3\n") + self.ed.SetSel(0, 9) + self.ed.LineReverse() + self.assertEquals(self.ed.Contents(), b"c3\nb2\na1\n") + def testMoveSelectedLines(self): lineEndType = self.ed.EOLMode self.ed.EOLMode = self.ed.SC_EOL_LF |