Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the case where first line is not indented, like python's dedent #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions dedent.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ func Dedent(text string) string {
indents := leadingWhitespace.FindAllStringSubmatch(text, -1)

// Look for the longest leading string of spaces and tabs common to all
// lines.
// lines, except first non-indented lines.
firstIndentedLine := 0
for i, indent := range indents {
if i == 0 {
if i == firstIndentedLine && indent[1] == "" {
// no indent in first line, ignore it
firstIndentedLine = 1
} else if i == firstIndentedLine {
margin = indent[1]
} else if strings.HasPrefix(indent[1], margin) {
// Current line more deeply indented than previous winner:
Expand Down