Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
eyelash committed Nov 5, 2023
1 parent 81cc991 commit 4cbf99a
Show file tree
Hide file tree
Showing 20 changed files with 67 additions and 67 deletions.
20 changes: 10 additions & 10 deletions atom/src/bracket-matcher-view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,28 +156,28 @@ std::pair<optional<Range>, optional<Range>> 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)), "</") == 0) {
} else if (strcmp(ts_node_type(ts_node_first_child(endTag)), "</") == 0) {
return {
rangeForNode(ts_node_child(*startTag, 1)),
rangeForNode(ts_node_child(*endTag, 1))
rangeForNode(ts_node_child(startTag, 1)),
rangeForNode(ts_node_child(endTag, 1))
};
} else {
return {
rangeForNode(ts_node_child(*startTag, 1)),
rangeForNode(ts_node_child(*endTag, 2))
rangeForNode(ts_node_child(startTag, 1)),
rangeForNode(ts_node_child(endTag, 2))
};
}
} else {
return {};
}
}

std::pair<optional<TSNode>, optional<TSNode>> BracketMatcherView::findContainingTagsWithSyntaxTree(Point position, TreeSitterLanguageMode *languageMode) {
optional<TSNode> startTag, endTag;
std::pair<TSNode, TSNode> 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) {
Expand Down
2 changes: 1 addition & 1 deletion atom/src/bracket-matcher-view.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct BracketMatcherView {
optional<Point> findMatchingEndBracketWithSyntaxTree(const Point &, char16_t, char16_t, TreeSitterLanguageMode *);
optional<Point> findMatchingStartBracketWithSyntaxTree(const Point &, char16_t, char16_t, TreeSitterLanguageMode *);
std::pair<optional<Range>, optional<Range>> findMatchingTagNameRangesWithSyntaxTree(TreeSitterLanguageMode *);
std::pair<optional<TSNode>, optional<TSNode>> findContainingTagsWithSyntaxTree(Point, TreeSitterLanguageMode *);
std::pair<TSNode, TSNode> findContainingTagsWithSyntaxTree(Point, TreeSitterLanguageMode *);
DisplayMarker *createMarker(const Range &);
std::tuple<optional<Point>, optional<Point>> findCurrentPair();
TreeSitterLanguageMode *hasSyntaxTree();
Expand Down
4 changes: 2 additions & 2 deletions atom/src/cursor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

Expand Down
2 changes: 1 addition & 1 deletion atom/src/text-editor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion superstring/src/native-point.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t>()},
Expand Down
6 changes: 3 additions & 3 deletions superstring/src/native-point.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion superstring/src/regex.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions text-buffer/src/display-layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -478,7 +477,7 @@ std::vector<double> 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);
Expand Down
6 changes: 3 additions & 3 deletions text-buffer/src/display-marker-layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion text-buffer/src/helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 9 additions & 7 deletions text-buffer/src/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,6 @@ template <typename T> class Slice {
}
};

template <typename CharT> bool includes(const std::basic_string<CharT> &haystack, const CharT *needle, size_t needle_length, size_t position = 0) {
return haystack.find(needle, position, needle_length) != std::basic_string<CharT>::npos;
}
template <typename CharT> bool includes(const std::basic_string<CharT> &haystack, CharT needle, size_t position = 0) {
return includes(haystack, &needle, 1, position);
}

std::u16string toUpperCase(std::u16string);
std::u16string toLowerCase(std::u16string);

Expand Down Expand Up @@ -111,6 +104,15 @@ template <typename CharT> std::vector<std::basic_string<CharT>> split(const std:

std::u16string escapeRegExp(const std::u16string &string);

template <typename K, typename V> V *get(const std::unordered_map<K, V *> &m, const K &k) {
auto i = m.find(k);
if (i != m.end()) {
return i->second;
} else {
return nullptr;
}
}

template <typename T> T pop(std::vector<T> &v) {
T result = std::move(v.back());
v.pop_back();
Expand Down
6 changes: 2 additions & 4 deletions text-buffer/src/marker-layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<Marker *> MarkerLayer::getMarkers() {
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions text-buffer/src/marker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ bool Marker::update(const Range &oldRange, const Params &params, 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;
Expand All @@ -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 &currentRange, bool textChanged, bool propertiesChanged) {
if (!this->hasChangeObservers) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions text-buffer/src/marker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
4 changes: 2 additions & 2 deletions text-buffer/src/screen-line-builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
8 changes: 4 additions & 4 deletions text-buffer/src/screen-line-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ struct ScreenLineBuilder {
ScreenLineBuilder(DisplayLayer *);
~ScreenLineBuilder();

std::vector<DisplayLayer::ScreenLine> buildScreenLines(double startScreenRow, double endScreenRow);
double getBuiltInScopeId(int32_t flags);
std::vector<DisplayLayer::ScreenLine> buildScreenLines(double, double);
double getBuiltInScopeId(int32_t);
void beginLine();
void updateCurrentTokenFlags(char16_t);
void emitDecorationBoundaries(LanguageMode::HighlightIterator &);
Expand All @@ -40,15 +40,15 @@ 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);
void emitOpenTag(double, bool = true);
void closeContainingScopes();
void reopenTags();
void pushScreenLine(const DisplayLayer::ScreenLine &);
double compareBufferPosition(Point);
double compareBufferPosition(const Point &);
};

#endif // SCREEN_LINE_BUILDER_H_
11 changes: 6 additions & 5 deletions text-buffer/src/text-buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -618,7 +619,7 @@ DisplayLayer *TextBuffer::addDisplayLayer() {
}

DisplayLayer *TextBuffer::getDisplayLayer(unsigned id) {
return this->displayLayers[id];
return get(this->displayLayers, id);
}

/*
Expand Down Expand Up @@ -793,7 +794,7 @@ void TextBuffer::markersUpdated(MarkerLayer *layer) {

unsigned TextBuffer::getNextMarkerId() { return this->nextMarkerId++; }

TextBuffer::SearchCallbackArgument::SearchCallbackArgument(TextBuffer *buffer, Range range, const Regex &regex) :
TextBuffer::SearchCallbackArgument::SearchCallbackArgument(TextBuffer *buffer, const Range &range, const Regex &regex) :
buffer{buffer},
range{range},
regex{regex},
Expand Down
2 changes: 1 addition & 1 deletion text-buffer/src/text-buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct TextBuffer {
const Regex &regex;
bool stopped;
optional<std::u16string> replacementText;
SearchCallbackArgument(TextBuffer *, Range, const Regex &);
SearchCallbackArgument(TextBuffer *, const Range &, const Regex &);
std::u16string getMatchText();
Range replace(std::u16string);
void stop();
Expand Down
14 changes: 7 additions & 7 deletions tree-sitter/src/tree-sitter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <native-point.h>
#include <native-text-buffer.h>

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) {
Expand All @@ -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));
}

Expand Down
12 changes: 6 additions & 6 deletions tree-sitter/src/tree-sitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 4cbf99a

Please sign in to comment.