Skip to content

Commit

Permalink
libshell: Byte/character offsets in text editor string manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent cae9e93 commit aeef0bb
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 103 deletions.
19 changes: 11 additions & 8 deletions doomsday/libs/shell/include/de/shell/abstractlineeditor.h
Expand Up @@ -44,6 +44,9 @@ class Lexicon;
*/
class LIBSHELL_PUBLIC AbstractLineEditor : public ITextEditor
{
public:
struct LineBytePos { BytePos x; int line; };

public:
AbstractLineEditor(ILineWrapping *lineWraps);

Expand All @@ -60,22 +63,22 @@ class LIBSHELL_PUBLIC AbstractLineEditor : public ITextEditor
void setText(String const &lineText);
String text() const;

void setCursor(int index);
int cursor() const;
void setCursor(BytePos index);
BytePos cursor() const;

/**
* Determines the position of a specific character on the wrapped lines.
* The Y component is the wrapped line index and the X component is the
* character index on that line.
*/
Vec2i linePos(int index) const;
LineBytePos linePos(BytePos index) const;

Vec2i lineCursorPos() const { return linePos(cursor()); }
LineBytePos lineCursorPos() const { return linePos(cursor()); }

bool isSuggestingCompletion() const;
Rangei completionRange() const;
StringList suggestedCompletions() const;
void acceptCompletion();
bool isSuggestingCompletion() const;
String::ByteRange completionRange() const;
StringList suggestedCompletions() const;
void acceptCompletion();

/**
* Defines the terms and rules for auto-completion.
Expand Down
6 changes: 3 additions & 3 deletions doomsday/libs/shell/include/de/shell/itexteditor.h
Expand Up @@ -34,10 +34,10 @@ class ITextEditor
virtual ~ITextEditor() {}

virtual void setText(String const &text) = 0;
virtual void setCursor(int pos) = 0;
virtual void setCursor(BytePos bytePos) = 0;

virtual String text() const = 0;
virtual int cursor() const = 0;
virtual String text() const = 0;
virtual BytePos cursor() const = 0;
};

}} // namespace de::shell
Expand Down
4 changes: 2 additions & 2 deletions doomsday/libs/shell/include/de/shell/libshell.h
Expand Up @@ -93,7 +93,7 @@ class LIBSHELL_PUBLIC ILineWrapping
virtual int height() const = 0;

/// Returns the advance width of the range.
virtual int rangeWidth(const String::ByteRange &range) const = 0;
virtual BytePos rangeWidth(const String::ByteRange &range) const = 0;

/**
* Calculates which index in the provided content range occupies a
Expand All @@ -102,7 +102,7 @@ class LIBSHELL_PUBLIC ILineWrapping
* @param range Range within the content.
* @param width Advance width to check.
*/
virtual int indexAtWidth(const String::ByteRange &range, int width) const = 0;
virtual BytePos indexAtWidth(const String::ByteRange &range, BytePos width) const = 0;
};

} // namespace shell
Expand Down
4 changes: 2 additions & 2 deletions doomsday/libs/shell/include/de/shell/monospacelinewrapping.h
Expand Up @@ -52,8 +52,8 @@ class LIBSHELL_PUBLIC MonospaceLineWrapping : public ILineWrapping
WrappedLine line(int index) const { return _lines[index]; }
int width() const;
int height() const;
int rangeWidth(const String::ByteRange &range) const;
int indexAtWidth(const String::ByteRange &range, int width) const;
BytePos rangeWidth(const String::ByteRange &range) const;
BytePos indexAtWidth(const String::ByteRange &range, BytePos width) const;

private:
List<WrappedLine> _lines;
Expand Down
14 changes: 7 additions & 7 deletions doomsday/libs/shell/include/de/shell/textcanvas.h
Expand Up @@ -158,7 +158,7 @@ class LIBSHELL_PUBLIC TextCanvas
void drawText(Coord const & pos,
String const & text,
AttribChar::Attribs const &attribs = AttribChar::DefaultAttributes,
int richOffset = 0);
BytePos richOffset = BytePos(0));

/**
* Draws line wrapped text. Use de::shell::wordWrapText() to determine
Expand All @@ -170,15 +170,15 @@ class LIBSHELL_PUBLIC TextCanvas
* @param attribs Character attributes.
* @param lineAlignment Alignment for lines.
*/
void drawWrappedText(Coord const & pos,
String const & text,
ILineWrapping const & wraps,
AttribChar::Attribs const &attribs = AttribChar::DefaultAttributes,
Alignment lineAlignment = AlignLeft);
void drawWrappedText(const Coord & pos,
const String & text,
const ILineWrapping & wraps,
const AttribChar::Attribs &attribs = AttribChar::DefaultAttributes,
const Alignment & lineAlignment = AlignLeft);

void clearRichFormat();

void setRichFormatRange(AttribChar::Attribs const &attribs, Rangei const &range);
void setRichFormatRange(AttribChar::Attribs const &attribs, const String::ByteRange &range);

void drawLineRect(Rectanglei const & rect,
AttribChar::Attribs const &attribs = AttribChar::DefaultAttributes);
Expand Down

0 comments on commit aeef0bb

Please sign in to comment.