Skip to content
Browse files

Implemented proper dedentation in `sonofaj/Doc`.

  • Loading branch information...
1 parent 3475757 commit 297baefe7dcc965fa51cc6777940aebea02de1bc @fredreichbier committed Feb 7, 2010
Showing with 30 additions and 12 deletions.
  1. +30 −12 source/sonofaj/Doc.ooc
View
42 source/sonofaj/Doc.ooc
@@ -1,21 +1,39 @@
-import text/StringBuffer
+import structs/ArrayList
+import text/[StringBuffer, StringTokenizer]
formatDoc: func (doc: String) -> String {
- afterNewline := true
- buf := StringBuffer new()
docStripped := doc substring(3, doc length() - 2) /* without / ** ... * / */
- for(c: Char in docStripped) {
- if(afterNewline) {
- if(c != ' ' && c != '*' && c != '\t') {
- afterNewline = false
+ lines := docStripped split('\n', true) toArrayList() as ArrayList<String>
+ maxIndentation := -1
+ // find longest common indentation
+ for(idx in 1..lines size()) {
+ line := lines[idx] as String
+ // calculate current indentation
+ indentation := 0
+ foundChar := false
+ for(chr in line) {
+ if(chr isWhitespace() || chr == '*') { // '*' is also whitespace.
+ indentation += 1
} else {
- continue
+ foundChar = true
+ break
}
}
- if(c == '\n') {
- afterNewline = true
+ // shorter than max indentation? but only if we got a real char.
+ if(foundChar) {
+ if(foundChar && (maxIndentation == -1 || indentation < maxIndentation)) {
+ maxIndentation = indentation
+ }
+ } else {
+ // haven't found real char? empty line.
+ lines[idx] = ""
}
- buf append(c)
}
- buf toString()
+ // now strip the calculated indentation
+ lines[0] = lines[0] trim("\t *") // the first line is just trimmed.
+ for(idx in 1..lines size()) {
+ if(!lines[idx] isEmpty())
+ lines[idx] = lines[idx] substring(maxIndentation)
+ }
+ lines join('\n')
}

0 comments on commit 297baef

Please sign in to comment.
Something went wrong with that request. Please try again.