From 085a3669716f19ecc96cac448b76994a82af33fb Mon Sep 17 00:00:00 2001 From: Elias Aebi Date: Mon, 16 Oct 2023 17:27:44 +0200 Subject: [PATCH] clean up --- atom/src/bracket-matcher-view.cc | 20 ++++++++++---------- atom/src/bracket-matcher-view.h | 2 +- atom/src/cursor.cc | 4 ++-- atom/src/text-editor.cc | 2 +- superstring/src/native-point.cc | 2 +- superstring/src/native-point.h | 6 +++--- superstring/src/regex.cc | 2 +- text-buffer/src/display-layer.cc | 7 +++---- text-buffer/src/display-marker-layer.cc | 6 +++--- text-buffer/src/helpers.cc | 2 +- text-buffer/src/helpers.h | 9 +++++++++ text-buffer/src/marker-layer.cc | 6 ++---- text-buffer/src/marker.cc | 4 ++-- text-buffer/src/marker.h | 4 ++-- text-buffer/src/screen-line-builder.cc | 4 ++-- text-buffer/src/screen-line-builder.h | 8 ++++---- text-buffer/src/text-buffer.cc | 11 ++++++----- text-buffer/src/text-buffer.h | 2 +- tree-sitter/src/tree-sitter.cc | 14 +++++++------- tree-sitter/src/tree-sitter.h | 12 ++++++------ 20 files changed, 67 insertions(+), 60 deletions(-) diff --git a/atom/src/bracket-matcher-view.cc b/atom/src/bracket-matcher-view.cc index 35731ae..4401ff9 100644 --- a/atom/src/bracket-matcher-view.cc +++ b/atom/src/bracket-matcher-view.cc @@ -156,19 +156,19 @@ std::pair, optional> BracketMatcherView::findMatchingTagN const auto tags = this->findContainingTagsWithSyntaxTree(position, languageMode); const auto &startTag = std::get<0>(tags); const auto &endTag = std::get<1>(tags); - if (startTag && (rangeForNode(*startTag).containsPoint(position) || rangeForNode(*endTag).containsPoint(position))) { - if (ts_node_eq(*startTag, *endTag)) { - const Range range = rangeForNode(ts_node_child(*startTag, 1)); + if (!ts_node_is_null(startTag) && (rangeForNode(startTag).containsPoint(position) || rangeForNode(endTag).containsPoint(position))) { + if (ts_node_eq(startTag, endTag)) { + const Range range = rangeForNode(ts_node_child(startTag, 1)); return {range, range}; - } else if (strcmp(ts_node_type(ts_node_first_child(*endTag)), ", optional> BracketMatcherView::findMatchingTagN } } -std::pair, optional> BracketMatcherView::findContainingTagsWithSyntaxTree(Point position, TreeSitterLanguageMode *languageMode) { - optional startTag, endTag; +std::pair BracketMatcherView::findContainingTagsWithSyntaxTree(Point position, TreeSitterLanguageMode *languageMode) { + TSNode startTag = {}, endTag = {}; if (position.column == this->editor->buffer->lineLengthForRow(position.row)) position.column--; languageMode->getSyntaxNodeAtPosition(position, [&](TSNode node, TreeSitterGrammar *) { if (strstr(ts_node_type(node), "element") && ts_node_child_count(node) > 0) { diff --git a/atom/src/bracket-matcher-view.h b/atom/src/bracket-matcher-view.h index ccc4e05..d4c97db 100644 --- a/atom/src/bracket-matcher-view.h +++ b/atom/src/bracket-matcher-view.h @@ -28,7 +28,7 @@ struct BracketMatcherView { optional findMatchingEndBracketWithSyntaxTree(const Point &, char16_t, char16_t, TreeSitterLanguageMode *); optional findMatchingStartBracketWithSyntaxTree(const Point &, char16_t, char16_t, TreeSitterLanguageMode *); std::pair, optional> findMatchingTagNameRangesWithSyntaxTree(TreeSitterLanguageMode *); - std::pair, optional> findContainingTagsWithSyntaxTree(Point, TreeSitterLanguageMode *); + std::pair findContainingTagsWithSyntaxTree(Point, TreeSitterLanguageMode *); DisplayMarker *createMarker(const Range &); std::tuple, optional> findCurrentPair(); TreeSitterLanguageMode *hasSyntaxTree(); diff --git a/atom/src/cursor.cc b/atom/src/cursor.cc index 154a9fc..acf87ab 100644 --- a/atom/src/cursor.cc +++ b/atom/src/cursor.cc @@ -100,8 +100,8 @@ bool Cursor::isBetweenWordAndNonWord() { const std::u16string nonWordCharacters = this->getNonWordCharacters(); return ( - includes(nonWordCharacters, text[0]) != - includes(nonWordCharacters, text[1]) + (nonWordCharacters.find(text[0]) != std::u16string::npos) != + (nonWordCharacters.find(text[1]) != std::u16string::npos) ); } diff --git a/atom/src/text-editor.cc b/atom/src/text-editor.cc index 9ee421c..fbaf4ce 100644 --- a/atom/src/text-editor.cc +++ b/atom/src/text-editor.cc @@ -1706,7 +1706,7 @@ void TextEditor::pasteText(/* options = {} */) { if ( indentBasis && - (includes(text, u'\n') || + (text.find(u'\n') != std::u16string::npos || !selection->cursor->hasPrecedingCharactersOnLine()) ) { options_indentBasis = *indentBasis; diff --git a/superstring/src/native-point.cc b/superstring/src/native-point.cc index 805133a..60b31fc 100644 --- a/superstring/src/native-point.cc +++ b/superstring/src/native-point.cc @@ -16,7 +16,7 @@ NativePoint NativePoint::max() { NativePoint::NativePoint() : NativePoint(0, 0) {} -NativePoint::NativePoint(unsigned row, unsigned column) : row{row}, column{column} {} +NativePoint::NativePoint(uint32_t row, uint32_t column) : row{row}, column{column} {} NativePoint::NativePoint(Deserializer &input) : row{input.read()}, diff --git a/superstring/src/native-point.h b/superstring/src/native-point.h index 94bc47a..61cf631 100644 --- a/superstring/src/native-point.h +++ b/superstring/src/native-point.h @@ -5,15 +5,15 @@ #include "serializer.h" struct NativePoint { - unsigned row; - unsigned column; + uint32_t row; + uint32_t column; static NativePoint min(const NativePoint &left, const NativePoint &right); static NativePoint max(const NativePoint &left, const NativePoint &right); static NativePoint max(); NativePoint(); - NativePoint(unsigned row, unsigned column); + NativePoint(uint32_t row, uint32_t column); NativePoint(Deserializer &input); int compare(const NativePoint &other) const; diff --git a/superstring/src/regex.cc b/superstring/src/regex.cc index 99acab4..ba2ced1 100644 --- a/superstring/src/regex.cc +++ b/superstring/src/regex.cc @@ -11,7 +11,7 @@ Regex::Regex() : code{nullptr} {} static u16string preprocess_pattern(const char16_t *pattern, uint32_t length) { u16string result; - for (unsigned i = 0; i < length;) { + for (uint32_t i = 0; i < length;) { char16_t c = pattern[i]; // Replace escape sequences like '\u00cf' with their literal UTF16 value diff --git a/text-buffer/src/display-layer.cc b/text-buffer/src/display-layer.cc index ac6e5ae..8cfb794 100644 --- a/text-buffer/src/display-layer.cc +++ b/text-buffer/src/display-layer.cc @@ -91,9 +91,8 @@ DisplayMarkerLayer *DisplayLayer::addMarkerLayer(bool maintainHistory) { } DisplayMarkerLayer *DisplayLayer::getMarkerLayer(unsigned id) { - auto iter = this->displayMarkerLayersById.find(id); - if (iter != this->displayMarkerLayersById.end()) { - return iter->second; + if (this->displayMarkerLayersById.count(id)) { + return this->displayMarkerLayersById[id]; } else { MarkerLayer *bufferMarkerLayer = this->buffer->getMarkerLayer(id); if (bufferMarkerLayer) { @@ -478,7 +477,7 @@ std::vector DisplayLayer::bufferRowsForScreenRows(double startRow, doubl double lastScreenRow = startRow; double lastBufferRow = this->translateScreenPositionWithSpatialIndex(startPosition).row; auto hunks = this->spatialIndex->grab_changes_in_new_range(startPosition, Point(endRow, 0)); - for (unsigned i = 0; i < hunks.size(); i++) { + for (size_t i = 0; i < hunks.size(); i++) { const auto &hunk = hunks[i]; while (lastScreenRow <= hunk.new_start.row) { bufferRows.push_back(lastBufferRow); diff --git a/text-buffer/src/display-marker-layer.cc b/text-buffer/src/display-marker-layer.cc index be6c712..b466637 100644 --- a/text-buffer/src/display-marker-layer.cc +++ b/text-buffer/src/display-marker-layer.cc @@ -58,7 +58,7 @@ DisplayMarker *DisplayMarkerLayer::markScreenPosition(const Point &screenPositio DisplayMarker *DisplayMarkerLayer::markBufferRange(const Range &bufferRange) { Marker *marker = this->bufferMarkerLayer->markRange(bufferRange); return marker ? this->getMarker(marker->id) : nullptr; - } +} DisplayMarker *DisplayMarkerLayer::markBufferPosition(const Point &bufferPosition) { Marker *marker = this->bufferMarkerLayer->markPosition(bufferPosition); @@ -70,8 +70,8 @@ Section: Querying */ DisplayMarker *DisplayMarkerLayer::getMarker(unsigned id) { - if (this->markersById.count(id)) { - return this->markersById[id]; + if (DisplayMarker *displayMarker = get(this->markersById, id)) { + return displayMarker; } else if (Marker *bufferMarker = this->bufferMarkerLayer->getMarker(id)) { return this->markersById[id] = new DisplayMarker(this, bufferMarker); } diff --git a/text-buffer/src/helpers.cc b/text-buffer/src/helpers.cc index 82883bb..34db76c 100644 --- a/text-buffer/src/helpers.cc +++ b/text-buffer/src/helpers.cc @@ -37,7 +37,7 @@ std::u16string toLowerCase(std::u16string string) { std::u16string escapeRegExp(const std::u16string &string) { std::u16string result; for (char16_t c : string) { - if (includes(std::u16string(u"-/\\^$*+?.()|[]{}"), c)) { + if (std::u16string(u"-/\\^$*+?.()|[]{}").find(c) != std::u16string::npos) { result.push_back(u'\\'); } result.push_back(c); diff --git a/text-buffer/src/helpers.h b/text-buffer/src/helpers.h index 72ff702..c42ff2c 100644 --- a/text-buffer/src/helpers.h +++ b/text-buffer/src/helpers.h @@ -111,6 +111,15 @@ template std::vector> split(const std: std::u16string escapeRegExp(const std::u16string &string); +template V *get(const std::unordered_map &m, const K &k) { + auto i = m.find(k); + if (i != m.end()) { + return i->second; + } else { + return nullptr; + } +} + template T pop(std::vector &v) { T result = std::move(v.back()); v.pop_back(); diff --git a/text-buffer/src/marker-layer.cc b/text-buffer/src/marker-layer.cc index 515a6cb..e8f6ea6 100644 --- a/text-buffer/src/marker-layer.cc +++ b/text-buffer/src/marker-layer.cc @@ -35,7 +35,7 @@ Section: Querying */ Marker *MarkerLayer::getMarker(unsigned id) { - return this->markersById.count(id) ? this->markersById[id] : nullptr; + return get(this->markersById, id); } std::vector MarkerLayer::getMarkers() { @@ -201,9 +201,7 @@ void MarkerLayer::restoreFromSnapshot(const Snapshot &snapshots, bool alwaysCrea this.createMarker(snapshot.range, snapshot, true); continue; }*/ - auto iter = this->markersById.find(id); - if (iter != this->markersById.end()) { - Marker *marker = iter->second; + if (Marker *marker = get(this->markersById, id)) { marker->update(marker->getRange(), {snapshot.second.range, snapshot.second.reversed, snapshot.second.tailed}, true, true); } else { //Marker *marker = snapshot.marker; diff --git a/text-buffer/src/marker.cc b/text-buffer/src/marker.cc index b63c21a..2e1984f 100644 --- a/text-buffer/src/marker.cc +++ b/text-buffer/src/marker.cc @@ -204,7 +204,7 @@ bool Marker::update(const Range &oldRange, const Params ¶ms, bool textChange return updated; } -Marker::Snapshot Marker::getSnapshot(Range range, bool includeMarker) { +Marker::Snapshot Marker::getSnapshot(const Range &range, bool includeMarker) { Snapshot snapshot {range, this->reversed, this->tailed, this->invalidate, this->exclusive}; if (includeMarker) { //snapshot.marker = this; @@ -216,7 +216,7 @@ Marker::Snapshot Marker::getSnapshot(Range range, bool includeMarker) { Section: Private */ -void Marker::emitChangeEvent(Range currentRange, bool textChanged, bool propertiesChanged) { +void Marker::emitChangeEvent(const Range ¤tRange, bool textChanged, bool propertiesChanged) { if (!this->hasChangeObservers) { return; } diff --git a/text-buffer/src/marker.h b/text-buffer/src/marker.h index 873111b..d616a62 100644 --- a/text-buffer/src/marker.h +++ b/text-buffer/src/marker.h @@ -62,8 +62,8 @@ struct Marker { void destroy(bool = false); int compare(Marker *); bool update(const Range &, const Params &, bool = false, bool = false); - Snapshot getSnapshot(Range, bool = true); - void emitChangeEvent(Range, bool, bool); + Snapshot getSnapshot(const Range &, bool = true); + void emitChangeEvent(const Range &, bool, bool); }; #endif // MARKER_H_ diff --git a/text-buffer/src/screen-line-builder.cc b/text-buffer/src/screen-line-builder.cc index d7fd827..2d0bc04 100644 --- a/text-buffer/src/screen-line-builder.cc +++ b/text-buffer/src/screen-line-builder.cc @@ -346,7 +346,7 @@ void ScreenLineBuilder::emitHardTab() { } } -void ScreenLineBuilder::emitText(std::u16string text, bool reopenTags) { +void ScreenLineBuilder::emitText(const std::u16string &text, bool reopenTags) { if (reopenTags) this->reopenTags(); this->currentScreenLineText += text; const double length = text.size(); @@ -430,7 +430,7 @@ void ScreenLineBuilder::pushScreenLine(const DisplayLayer::ScreenLine &screenLin } } -double ScreenLineBuilder::compareBufferPosition(Point position) { +double ScreenLineBuilder::compareBufferPosition(const Point &position) { const double rowComparison = this->bufferPosition.row - position.row; return rowComparison == 0 ? (this->bufferPosition.column - position.column) : rowComparison; } diff --git a/text-buffer/src/screen-line-builder.h b/text-buffer/src/screen-line-builder.h index 3c5ac0d..30ad0e3 100644 --- a/text-buffer/src/screen-line-builder.h +++ b/text-buffer/src/screen-line-builder.h @@ -29,8 +29,8 @@ struct ScreenLineBuilder { ScreenLineBuilder(DisplayLayer *); ~ScreenLineBuilder(); - std::vector buildScreenLines(double startScreenRow, double endScreenRow); - double getBuiltInScopeId(int32_t flags); + std::vector buildScreenLines(double, double); + double getBuiltInScopeId(int32_t); void beginLine(); void updateCurrentTokenFlags(char16_t); void emitDecorationBoundaries(LanguageMode::HighlightIterator &); @@ -40,7 +40,7 @@ struct ScreenLineBuilder { void emitNewline(double = -1); void emitIndentWhitespace(double); void emitHardTab(); - void emitText(std::u16string, bool = true); + void emitText(const std::u16string &, bool = true); void emitTokenBoundary(); void emitEmptyTokenIfNeeded(); void emitCloseTag(int32_t); @@ -48,7 +48,7 @@ struct ScreenLineBuilder { void closeContainingScopes(); void reopenTags(); void pushScreenLine(const DisplayLayer::ScreenLine &); - double compareBufferPosition(Point); + double compareBufferPosition(const Point &); }; #endif // SCREEN_LINE_BUILDER_H_ diff --git a/text-buffer/src/text-buffer.cc b/text-buffer/src/text-buffer.cc index fa81ca1..f65d446 100644 --- a/text-buffer/src/text-buffer.cc +++ b/text-buffer/src/text-buffer.cc @@ -193,9 +193,10 @@ Range TextBuffer::setText(const std::u16string &text) { Range TextBuffer::setTextInRange(const Range &range, const std::u16string &newText) { if (this->transactCallDepth == 0) { - //const Range newRange = this->transact([&]() { this->setTextInRange(range, newText /* , {normalizeLineEndings} */); }); + Range newRange; + this->transact([&]() { newRange = this->setTextInRange(range, newText /* , {normalizeLineEndings} */); }); //if (undo === 'skip') this.groupLastChanges() - //return newRange; + return newRange; } const Range oldRange = this->clipRange(range); @@ -313,7 +314,7 @@ MarkerLayer *TextBuffer::addMarkerLayer(bool maintainHistory) { } MarkerLayer *TextBuffer::getMarkerLayer(unsigned id) { - return this->markerLayers[id]; + return get(this->markerLayers, id); } MarkerLayer *TextBuffer::getDefaultMarkerLayer() { @@ -618,7 +619,7 @@ DisplayLayer *TextBuffer::addDisplayLayer() { } DisplayLayer *TextBuffer::getDisplayLayer(unsigned id) { - return this->displayLayers[id]; + return get(this->displayLayers, id); } /* @@ -793,7 +794,7 @@ void TextBuffer::markersUpdated(MarkerLayer *layer) { unsigned TextBuffer::getNextMarkerId() { return this->nextMarkerId++; } -TextBuffer::SearchCallbackArgument::SearchCallbackArgument(TextBuffer *buffer, Range range, const Regex ®ex) : +TextBuffer::SearchCallbackArgument::SearchCallbackArgument(TextBuffer *buffer, const Range &range, const Regex ®ex) : buffer{buffer}, range{range}, regex{regex}, diff --git a/text-buffer/src/text-buffer.h b/text-buffer/src/text-buffer.h index 2c9d275..ffb6f94 100644 --- a/text-buffer/src/text-buffer.h +++ b/text-buffer/src/text-buffer.h @@ -46,7 +46,7 @@ struct TextBuffer { const Regex ®ex; bool stopped; optional replacementText; - SearchCallbackArgument(TextBuffer *, Range, const Regex &); + SearchCallbackArgument(TextBuffer *, const Range &, const Regex &); std::u16string getMatchText(); Range replace(std::u16string); void stop(); diff --git a/tree-sitter/src/tree-sitter.cc b/tree-sitter/src/tree-sitter.cc index d4879a7..14eb6d1 100644 --- a/tree-sitter/src/tree-sitter.cc +++ b/tree-sitter/src/tree-sitter.cc @@ -2,7 +2,7 @@ #include #include -static const unsigned BYTES_PER_CHARACTER = 2; +static const uint32_t BYTES_PER_CHARACTER = 2; static TSTreeCursor scratch_cursor = {nullptr, nullptr, {0, 0}}; static inline bool operator<=(const TSPoint &left, const TSPoint &right) { @@ -19,27 +19,27 @@ static TSPoint PointFromJS(const NativePoint &point) { return {point.row, point.column * BYTES_PER_CHARACTER}; } -unsigned startIndex(TSNode node) { +uint32_t startIndex(TSNode node) { return ts_node_start_byte(node) / BYTES_PER_CHARACTER; } -unsigned endIndex(TSNode node) { +uint32_t endIndex(TSNode node) { return ts_node_end_byte(node) / BYTES_PER_CHARACTER; } -unsigned startIndex(TSRange range) { +uint32_t startIndex(TSRange range) { return range.start_byte / BYTES_PER_CHARACTER; } -unsigned endIndex(TSRange range) { +uint32_t endIndex(TSRange range) { return range.end_byte / BYTES_PER_CHARACTER; } -unsigned startIndex(TSTreeCursor *tree_cursor) { +uint32_t startIndex(TSTreeCursor *tree_cursor) { return startIndex(ts_tree_cursor_current_node(tree_cursor)); } -unsigned endIndex(TSTreeCursor *tree_cursor) { +uint32_t endIndex(TSTreeCursor *tree_cursor) { return endIndex(ts_tree_cursor_current_node(tree_cursor)); } diff --git a/tree-sitter/src/tree-sitter.h b/tree-sitter/src/tree-sitter.h index c33963d..6212b85 100644 --- a/tree-sitter/src/tree-sitter.h +++ b/tree-sitter/src/tree-sitter.h @@ -6,12 +6,12 @@ struct NativeTextBuffer; -unsigned startIndex(TSNode); -unsigned endIndex(TSNode); -unsigned startIndex(TSRange); -unsigned endIndex(TSRange); -unsigned startIndex(TSTreeCursor *); -unsigned endIndex(TSTreeCursor *); +uint32_t startIndex(TSNode); +uint32_t endIndex(TSNode); +uint32_t startIndex(TSRange); +uint32_t endIndex(TSRange); +uint32_t startIndex(TSTreeCursor *); +uint32_t endIndex(TSTreeCursor *); NativePoint startPosition(TSNode); NativePoint endPosition(TSNode); NativePoint startPosition(TSRange);