aboutsummaryrefslogtreecommitdiff
path: root/formatter.go
blob: 3d4e8730f188dc125324cba5487b9473ddbb5058 (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
package main

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

type CommentMode int

const (
	CommentsFollow CommentMode = iota
	CommentsPrecede
	CommentsStandalone
)

type Formatter struct {
	CommentMode CommentMode
}

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)}

	return []byte(formattingEngine.FormatToString(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)
	}
}