Skip to content

Commit

Permalink
Add additional methods for inspecting the indentation level
Browse files Browse the repository at this point in the history
  • Loading branch information
Verseth committed Jul 29, 2023
1 parent e8b7384 commit c162d83
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 27 additions & 6 deletions document.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand All @@ -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.
Expand Down

0 comments on commit c162d83

Please sign in to comment.