Skip to content

Commit

Permalink
Implement comment toggling
Browse files Browse the repository at this point in the history
See #34.
  • Loading branch information
magiblot committed Oct 15, 2022
1 parent 721be80 commit 395c02f
Show file tree
Hide file tree
Showing 14 changed files with 817 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
!include/**/
!source/
!source/**/
!test/
!test/**/

!*.*
*~/
Expand Down
23 changes: 22 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
cmake_minimum_required (VERSION 3.5)

option(TURBO_BUILD_APP "Build main editor application" ON)
option(TURBO_BUILD_TESTS "Build and run tests" OFF)
option(TURBO_BUILD_EXAMPLES "Build example apps" OFF)
option(TURBO_USE_SYSTEM_DEPS "Use system-wide dependencies instead of submodules (except Turbo Vision)" OFF)
option(TURBO_USE_SYSTEM_TVISION "Use system-wide Turbo Vision instead of the submodule" OFF)
Expand Down Expand Up @@ -150,6 +151,24 @@ if (TURBO_BUILD_APP)
install(TARGETS ${TURBO} RUNTIME DESTINATION bin)
endif()

# Target 'tests'

if (TURBO_BUILD_TESTS)
file(GLOB_RECURSE TEST_SRC "${CMAKE_CURRENT_LIST_DIR}/test/*.cc")
message(WARNING ${TEST_SRC})
add_executable(${TURBO}-test ${TEST_SRC})
target_compile_features(${TURBO}-test PRIVATE cxx_std_17)
turbo_set_warnings(${TURBO}-test)
find_library(GTEST gtest REQUIRED)
find_library(GTEST_MAIN gtest_main REQUIRED)
target_link_libraries(${TURBO}-test PRIVATE
${TURBO}-core
${GTEST}
${GTEST_MAIN}
)
add_custom_command(TARGET ${TURBO}-test POST_BUILD COMMAND ${TURBO}-test WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
endif()

# Examples

add_subdirectory(source/examples)
Expand All @@ -164,5 +183,7 @@ if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.16.0" AND TURBO_OPTIMIZE_BUILD)
if (TURBO_BUILD_APP)
set_target_properties(${TURBO} PROPERTIES UNITY_BUILD ON)
endif()

if (TURBO_BUILD_TESTS)
set_target_properties(${TURBO}-test PROPERTIES UNITY_BUILD ON)
endif()
endif()
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Some of the default keybindings are:
* `Ctrl+X`/`Shift+Del`: cut.
* `Ctrl+Z`, `Ctrl+Y`: undo/redo.
* `Tab`, `Shift+Tab`: indent/unindent.
* `Ctrl+Q`: toggle comment.
* `Ctrl+A`: select all.
* `Shift+Arrow`: extend selection.
* `Ctrl+F`: find.
Expand All @@ -89,7 +90,7 @@ Some of the default keybindings are:
* `Ctrl+S`: save document.
* `Ctrl+W`: close focused document.
* `F6`, `Shift+F6`: next/previous document (in MRU order).
* `Alt+X`: exit the application (I prefer this over `Ctrl+Q`, which is too close to `Ctrl+A` and `Ctrl+W`).
* `Alt+X`: exit the application.

In environments with extended keyboard support (e.g. the Linux console, Windows or Kitty ≥ 0.20.0), the following key shortcuts may also work:

Expand Down Expand Up @@ -128,6 +129,7 @@ Below is my TO-DO list of features I would like to implement in Turbo:
- [ ] Color scheme customization.
- [x] Syntax highlighting for some languages (C/C++, Rust, Python, JavaScript, Make, Bash, Ruby, JSON, YAML).
- [ ] Syntax highlighting for the rest of [languages supported by Scintilla](https://github.com/RaiKoHoff/scintilla/blob/master/include/SciLexer.h).
- [x] Comment toggling.
- [x] Brace match highlighting.
- [ ] VIM input mode.
- [ ] Localization.
Expand Down
45 changes: 45 additions & 0 deletions include/test/test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef TURBO_TEST_H
#define TURBO_TEST_H

#include <gtest/gtest.h>

#include <turbo/scintilla/internals.h>
#include <turbo/scintilla.h>

namespace turbo
{

template <class T>
struct TestCase
{
T input;
T result;
};

template <class T1, class T2>
inline void expectMatchingResult(const T1 &actual, const TestCase<T2> &testCase)
{
EXPECT_EQ(actual, testCase.result) << "With test input:\n" << testCase.input;
}

struct TextState
{
enum : char {
chCaret = '|',
chAnchor = '^',
};

std::string text;
Sci::Position caret {-1};
Sci::Position anchor {-1};

static TextState decode(std::string_view);
static std::string encode(TextState);
};

TScintilla &createScintilla(TextState state);
TextState getTextState(TScintilla &scintilla);

} // namespace turbo

#endif // TURBO_TEST_H
6 changes: 6 additions & 0 deletions include/turbo/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class Editor : protected TScintillaParent
inline void uppercase();
inline void lowercase();
inline void capitalize();
inline void toggleComment();
};

template <class Func>
Expand Down Expand Up @@ -138,6 +139,11 @@ inline void Editor::capitalize()
turbo::changeCaseOfSelection(scintilla, caseConvCapitalize);
}

inline void Editor::toggleComment()
{
turbo::toggleComment(scintilla, language);
}

class EditorView : public TSurfaceView
{
// 'EditorView' is used to display an 'Editor's contents and feed input
Expand Down
3 changes: 3 additions & 0 deletions include/turbo/editstates.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ void applyTheming(const LexerSettings *lexer, const ColorScheme *scheme, TScinti
// Highlights matching braces if there are any.
void updateBraces(const ColorScheme *scheme, TScintilla &scintilla);

// Toggles comment in selected text.
void toggleComment(TScintilla &scintilla, const Language *language);

void stripTrailingSpaces(TScintilla &scintilla);
void ensureNewlineAtEnd(TScintilla &scintilla);

Expand Down
20 changes: 14 additions & 6 deletions include/turbo/styles.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,21 @@

namespace turbo {

class Language
struct Language
{
public:
TStringView lineComment {};
TStringView blockCommentOpen {};
TStringView blockCommentClose {};

constexpr bool hasLineComments() const
{
return !lineComment.empty();
}

constexpr bool hasBlockComments() const
{
return !blockCommentOpen.empty() && !blockCommentClose.empty();
}

static const Language
CPP,
Expand Down Expand Up @@ -39,10 +51,6 @@ class Language
Erlang,
Smalltalk,
Markdown;

private:

Language() = default;
};

enum TextStyle : uchar
Expand Down
Loading

0 comments on commit 395c02f

Please sign in to comment.