diff --git a/CHANGELOG.md b/CHANGELOG.md index e0be9012..f881ae48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.1.3] - 29.07.2023 + +[Diff](https://github.com/elk-language/go-prompt/compare/v1.1.2...elk-language:go-prompt:v1.1.3) + +### Added +- `func (*prompt.Document).IndentSpaces(input string) int` +- `func (*prompt.Document).IndentLevel(input string, indentSize int) int` +- `func (*prompt.Document).CurrentLineIndentSpaces() int` +- `func (*prompt.Document).CurrentLineIndentLevel(indentSize int) int` + ## [1.1.2] - 29.07.2023 [Diff](https://github.com/elk-language/go-prompt/compare/v1.1.1...elk-language:go-prompt:v1.1.2) diff --git a/document.go b/document.go index 9ca57f1e..59e973b3 100644 --- a/document.go +++ b/document.go @@ -63,9 +63,8 @@ func (d *Document) CurrentRuneIndex() istrings.RuneNumber { } // Returns the amount of spaces that the last line of input -// is indented with. -func (d *Document) LastLineIndentSpaces() int { - input := d.Text +// of the given text is indented with. +func (d *Document) IndentSpaces(input string) int { lastNewline := strings.LastIndexByte(input, '\n') var spaces int for i := lastNewline + 1; i < len(input); i++ { @@ -80,12 +79,34 @@ func (d *Document) LastLineIndentSpaces() int { return spaces } -// Returns the indentation level of the last line of input. -func (d *Document) LastLineIndentLevel(indentSize int) int { +// Returns the indentation level of the last line of the given text. +func (d *Document) IndentLevel(input string, indentSize int) int { if indentSize == 0 { return 0 } - return d.LastLineIndentSpaces() / indentSize + return d.IndentSpaces(input) / indentSize +} + +// Returns the amount of spaces that the last line of input +// is indented with. +func (d *Document) LastLineIndentSpaces() int { + return d.IndentSpaces(d.Text) +} + +// Returns the indentation level of the last line of input. +func (d *Document) LastLineIndentLevel(indentSize int) int { + return d.IndentLevel(d.Text, indentSize) +} + +// Returns the amount of spaces that the current line the cursor is on +// is indented with. +func (d *Document) CurrentLineIndentSpaces() int { + return d.IndentSpaces(d.TextBeforeCursor()) +} + +// Returns the indentation level of the current line the cursor is on. +func (d *Document) CurrentLineIndentLevel(indentSize int) int { + return d.IndentLevel(d.TextBeforeCursor(), indentSize) } // TextBeforeCursor returns the text before the cursor.