aboutsummaryrefslogtreecommitdiff
path: root/formatter.go
blob: a82d619826672ba42fd15b4ce58953f4a87e8400 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main

import (
	"github.com/Fuwn/iku/engine"
	"path/filepath"
)

type CommentMode int

const (
	CommentsFollow CommentMode = iota
	CommentsPrecede
	CommentsStandalone
)

type Formatter struct {
	CommentMode   CommentMode
	Configuration Configuration
}

type lineInformation struct {
	statementType string
	isTopLevel    bool
	isScoped      bool
	isStartLine   bool
}

func (f *Formatter) Format(source []byte, filename string) ([]byte, error) {
	_, events, err := analyzeSource(source, filename)

	if err != nil {
		return nil, err
	}

	formattingEngine := &engine.Engine{
		CommentMode:           MapCommentMode(f.CommentMode),
		GroupSingleLineScopes: f.Configuration.GroupSingleLineFunctions,
	}

	return formattingEngine.FormatToBytes(events), nil
}

func analyzeSource(source []byte, filename string) ([]byte, []engine.LineEvent, error) {
	switch filepath.Ext(filename) {
	case ".js", ".ts", ".jsx", ".tsx":
		return (&EcmaScriptAdapter{}).Analyze(source)
	default:
		return (&GoAdapter{}).Analyze(source)
	}
}