1package main 2 3import "strings" 4 5// smartIndentation takes the leading whitespace for a line, and the trimmed contents of a line 6// it tries to indent or dedent in a smart way, intended for use on the following line, 7// and returns a new string of leading whitespace. 8func (e *Editor) smartIndentation(leadingWhitespace, trimmedLine string, alsoDedent bool) string { 9 // Grab the whitespace for this new line 10 // "smart indentation", add one indentation from the line above 11 if len(trimmedLine) > 0 && 12 (strings.HasSuffix(trimmedLine, "(") || strings.HasSuffix(trimmedLine, "{") || strings.HasSuffix(trimmedLine, "[") || 13 strings.HasSuffix(trimmedLine, ":")) && !strings.HasPrefix(trimmedLine, e.SingleLineCommentMarker()) { 14 leadingWhitespace += e.tabsSpaces.String() 15 } 16 if alsoDedent { 17 // "smart dedentation", subtract one indentation from the line above 18 if len(trimmedLine) > 0 && 19 (strings.HasSuffix(trimmedLine, ")") || strings.HasSuffix(trimmedLine, "}") || strings.HasSuffix(trimmedLine, "]")) { 20 indentation := e.tabsSpaces.String() 21 if len(leadingWhitespace) > len(indentation) { 22 leadingWhitespace = leadingWhitespace[:len(leadingWhitespace)-len(indentation)] 23 } 24 } 25 } 26 return leadingWhitespace 27} 28