Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repos:
exclude: "tests/fixture/no_newline_file.txt"
- id: trailing-whitespace
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v21.1.8
rev: v22.1.3
hooks:
- id: clang-format
exclude: ".json"
Expand Down Expand Up @@ -41,11 +41,11 @@ repos:
hooks:
- id: flake8
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.19.1
rev: v1.20.0
hooks:
- id: mypy
- repo: https://github.com/rhysd/actionlint
rev: v1.7.10
rev: v1.7.12
hooks:
- id: actionlint
exclude: "^.github/workflows/build_artifact.yml"
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### main / head

* `>` now intends the current line.
* `<` now dedents current line
* Implement `;!` to execute a shell command from the command bar
* Disabled continuous deployment in GHA. See `.github/workflows/build_artifact.yml`
for details
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ perform the following actions (alphabetically ordered):
| <kbd>_</Kbd> | Go to beginning of line |
| <kbd>$</Kbd> | Go to end of line |
| <kbd>></kbd> | Indent current line |
| <kbd><</kbd> | Dedent current line |

### Command mode
Likewise, you can switch to `command` mode with the semicolon `;` key. The
Expand Down
2 changes: 1 addition & 1 deletion run.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def test(testname: str | None, asan: bool) -> int:

create_read_only_file()
testname = testname if testname is not None else ""
test_flags: str = "-sr compact --order rand"
test_flags: str = "-r compact --order rand"
shell_cmd: str = f"./build/tests/test_exe {test_flags} {testname}"

return run_shell_cmd(
Expand Down
18 changes: 15 additions & 3 deletions src/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ enum class ActionType {
// Pass no values
Backspace,
CenterCurrentLine,
DedentLine,
DelCurrentChar,
DelCurrentLine,
DelCurrentWord,
Expand Down Expand Up @@ -110,6 +111,16 @@ template <typename T, typename U>
v->center_current_line();
} break;

case ActionType::DedentLine: {
auto logger = spdlog::get("basic_logger");
if (logger != nullptr) { logger->info("Action called: DedentLine"); }

v->get_active_model()->undo_stack.push_back(
Change(ActionType::DedentLine, v->get_active_model()->current_line, ' '));

v->get_active_model()->dedent_curr_line();
} break;

case ActionType::DelCurrentChar: {
if constexpr (std::is_same_v<U, Redraw>) {
auto logger = spdlog::get("basic_logger");
Expand Down Expand Up @@ -389,11 +400,12 @@ template <typename T, typename U>
if constexpr (std::is_same_v<T, char>) {
auto ret = v->get_active_model()->find_prev(action.payload);
if (ret.has_value()) {
for (int i = 0; i < ret.value().vertical; i++) {
const uint_t curr_char = v->get_active_model()->current_char;
const std::size_t steps =
v->get_active_model()->current_line - ret.value().vertical;
for (std::size_t i = 0; i < steps; i++) {
v->cursor_up();
}

const uint_t curr_char = v->get_active_model()->current_char;
if (ret.value().horizontal > int32_t(v->get_active_model()->current_char)) {
v->cursor_right(uint32_t(ret.value().horizontal) - curr_char);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

const bool LINE_NUMBERS = true;
const int TAB_SIZE = 4;
const std::size_t LINE_BORDER = 80;
const std::size_t LINE_BORDER = 100;

const rawterm::Color COLOR_UI_BG = rawterm::Colors::gray;
const rawterm::Color COLOR_DARK_YELLOW = rawterm::Color("#ffdd33");
Expand Down
6 changes: 6 additions & 0 deletions src/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,12 @@ void Controller::start_action_engine() {
parse_action<void, None>(&view, Action<void> {ActionType::ToggleCase});
view.draw_line(Draw_Line_dir::None);

// Dedent line
} else if (k.value() == rawterm::Key('<')) {
if (is_readonly_model()) { continue; }
parse_action<void, None>(&view, Action<void> {ActionType::DedentLine});
view.draw_line(Draw_Line_dir::None);

// Increment line
} else if (k.value() == rawterm::Key('>')) {
if (is_readonly_model()) { continue; }
Expand Down
41 changes: 27 additions & 14 deletions src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ void Model::toggle_case() {
unsaved = true;
}

[[nodiscard]] std::optional<rawterm::Pos> Model::find_next(const char c) {
[[nodiscard]] std::optional<rawterm::Pos> Model::find_next(const char c) const {
unsigned int cur_line = current_line;
int cur_char = int32_t(current_char);

Expand All @@ -262,24 +262,27 @@ void Model::toggle_case() {
return {};
}

[[nodiscard]] std::optional<rawterm::Pos> Model::find_prev(const char c) {
[[nodiscard]] std::optional<rawterm::Pos> Model::find_prev(const char c) const {
unsigned int cur_line = current_line;
int cur_char = int32_t(current_char);

for (; cur_line && cur_line < buf.size(); cur_line--) {
if (!(cur_line == current_line)) { cur_char = int32_t(buf.at(cur_line).size() - 1); }

auto iter = std::find(
buf.at(cur_line).rbegin() + int32_t(buf.at(cur_line).size()) - cur_char,
buf.at(cur_line).rend(), c);
while (cur_line) {
if (cur_line == current_line) {
const std::string_view search_area =
std::string_view(buf.at(current_line)).substr(0, current_char);
const auto pos = search_area.find_last_of(c);
if (pos != std::string::npos) {
return rawterm::Pos(static_cast<int>(current_line), static_cast<int>(pos));
}

if (iter != buf.at(cur_line).rend()) {
cur_char = int32_t(std::distance(buf.at(cur_line).begin(), iter.base() - 1));
} else {
const auto pos = buf.at(cur_line).find_last_of(c);

// line is a relative value, char is an absolute value
return rawterm::Pos(
{static_cast<int>(current_line - cur_line), static_cast<int>(cur_char)});
if (pos != std::string::npos) {
return rawterm::Pos(static_cast<int>(cur_line), static_cast<int>(pos));
}
}

cur_line--;
}

return {};
Expand Down Expand Up @@ -533,3 +536,13 @@ std::optional<rawterm::Pos> Model::find_next_str(std::string_view sv) {
void Model::indent_curr_line() {
if (buf.at(current_line).size()) { buf.at(current_line).insert(0, TAB_SIZE, ' '); }
}

void Model::dedent_curr_line() {
auto offset = buf.at(current_line).find_first_not_of(' ');
if (offset == 0) { return; }
unsaved = true;

const std::size_t to_delete = std::min(4ul, offset);
const std::size_t line_size = buf.at(current_line).size();
buf.at(current_line) = buf.at(current_line).substr(to_delete, line_size);
}
5 changes: 3 additions & 2 deletions src/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ struct Model {
[[nodiscard]] std::optional<int> end_of_word_pos();
void replace_char(const char);
void toggle_case();
[[nodiscard]] std::optional<rawterm::Pos> find_next(const char);
[[nodiscard]] std::optional<rawterm::Pos> find_prev(const char);
[[nodiscard]] std::optional<rawterm::Pos> find_next(const char) const;
[[nodiscard]] std::optional<rawterm::Pos> find_prev(const char) const;
[[nodiscard]] bool undo(const int);
[[nodiscard]] char get_current_char() const;
[[nodiscard]] bool redo(const int);
Expand All @@ -72,6 +72,7 @@ struct Model {
void search_and_replace(const std::string&);
std::optional<rawterm::Pos> find_next_str(std::string_view);
void indent_curr_line();
void dedent_curr_line();
};

#endif // MODEL_H
1 change: 1 addition & 0 deletions tests/fixture/test_file_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
11 changes: 11 additions & 0 deletions tests/integration/normal_mode_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,17 @@ def test_e_key(r: TmuxRunner):
assert r.statusbar_parts()[-1] == "1:7"


@setup("tests/fixture/test_file_1.txt")
def test_left_chevron_key(r: TmuxRunner):
r.press('<')
assert "[X]" not in r.statusbar_parts()

r.press('j')
r.press('<')
assert "[X]" in r.statusbar_parts()
assert "2\u2502here" in r.lines()[1]


@setup("tests/fixture/test_file_1.txt")
def test_right_chevron_key(r: TmuxRunner):
r.press('>')
Expand Down
27 changes: 23 additions & 4 deletions tests/model_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,15 +339,18 @@ TEST_CASE("find_prev", "[model]") {

auto ret = m.find_prev('f');
REQUIRE(ret.has_value());
REQUIRE(ret.value().vertical == 0);
REQUIRE(ret.value().vertical == 5);
REQUIRE(ret.value().horizontal == 5);

m.current_line -= static_cast<unsigned int>(ret.value().vertical);
m.current_char -= static_cast<unsigned int>(ret.value().horizontal);
m.current_line = static_cast<unsigned int>(ret.value().vertical);
m.current_char = static_cast<unsigned int>(ret.value().horizontal);
REQUIRE(m.current_line == 5);
REQUIRE(m.current_char == 5);
REQUIRE(m.buf.at(5).at(5) == 'f');

ret = m.find_prev('t');
REQUIRE(ret.has_value());
REQUIRE(ret.value().vertical == 3);
REQUIRE(ret.value().vertical == 2);
REQUIRE(ret.value().horizontal == 5);

m.current_line -= static_cast<unsigned int>(ret.value().vertical);
Expand Down Expand Up @@ -713,3 +716,19 @@ TEST_CASE("indent_curr_line", "[model]") {
REQUIRE(m.buf.at(0).at(0) == ' ');
REQUIRE(m.buf.at(0).at(4) == 'f');
}

TEST_CASE("dedent_curr_line", "[model]") {
auto m = Model({" foo", "bar"}, "");

// Dedent a line that has some indentation
m.dedent_curr_line();
REQUIRE(m.buf.at(0).size() == 3);
REQUIRE(m.buf.at(1).size() == 3);
REQUIRE(m.buf.at(0).at(0) == 'f');

// Dedent a line that has no indentation
m.dedent_curr_line();
REQUIRE(m.buf.at(0).size() == 3);
REQUIRE(m.buf.at(1).size() == 3);
REQUIRE(m.buf.at(0).at(0) == 'f');
}
Loading