Ctrl K
ComunidadGuías…

themes/indent.go

Rama main · réplica de lectura del repositorio original.

Archivo original ↗Preguntar a KiBot
package themes

import (
	"bytes"
)

// OneLevelOfIndentation finds one level of whitespace, given indented data
// and a keyword to extract the whitespace in front of.
//
// Returns either an empty string or the whitespace that represents one step of
// indentation in the given source code data.
func OneLevelOfIndentation(data *[]byte, keyword string) string {
	whitespace := ""
	kwb := []byte(keyword)
	// If there is a line that contains the given word, extract the whitespace
	if bytes.Contains(*data, kwb) {
		// Find the line that contains they keyword
		var byteline []byte
		found := false
		// Try finding the line with keyword, using \n as the newline
		for byteline = range bytes.SplitSeq(*data, []byte("\n")) {
			if bytes.Contains(byteline, kwb) {
				found = true
				break
			}
		}
		if found {
			// Find the whitespace in front of the keyword
			whitespaceBytes := byteline[:bytes.Index(byteline, kwb)]
			// Whitespace for one level of indentation
			whitespace = string(whitespaceBytes)
		}
	}
	// Return an empty string, or whitespace for one level of indentation
	return whitespace
}