golines

package module
v0.0.0-...-b35a614 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 17, 2025 License: MIT Imports: 13 Imported by: 2

README

Fork of golines

This is a fork of golines to be usable as a library.

I considered other options before deciding to fork, but there are no straightforward or non-invasive changes.

Simply isolating some code within a package will not address the root issue unless the code is converted to a dedicated module and all debug/graph-related code is removed. Realistically, this is not possible.

Issues should be open either on the original golines repository or on golangci-lint repository.

No modifications will be accepted other than the synchronization of the fork.

The synchronization of the fork will be done by the golangci-lint maintainers only.

Modifications

  • The original code is under the main package -> uses golines package.
  • Some files have been removed to reduce dependencies:
    • main.go, main_test.go
    • graph.go, graph_generated.go, graph.test
    • diff.go, diff_test.go
    • doc.go
  • Some other files have been removed because unused by the fork:
    • .goreleaser.yaml, .pre-commit-hooks.yaml
    • .github/build.yml, .github/release.yml
    • Makefile
  • The workflow files (lint.yml and test.yml) are modified to run for this fork.
  • The file shortener.go has been modified:
    • The baseFormatterCmd is hardcoded.
    • The code related to debug logs has been removed.
    • The code related to graph has been removed.
  • The module name has been changed to github.com/golangci/golines to avoid replacement directives inside golangci-lint.

History

  • sync with fc305205784a70b4cfc17397654f4c94e3153ce4 (after v0.12.2)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateAnnotation

func CreateAnnotation(length int) string

CreateAnnotation generates the text of a comment that will annotate long lines.

func FormatStructTags

func FormatStructTags(fieldList *dst.FieldList)

FormatStructTags formats struct tags so that the keys within each block of fields are aligned. It's not technically a shortening (and it usually makes these tags longer), so it's being kept separate from the core shortening logic for now.

See the struct_tags fixture for examples.

func HasAnnotation

func HasAnnotation(node dst.Node) bool

HasAnnotation determines whether the given AST node has a line length annotation on it.

func HasAnnotationRecursive

func HasAnnotationRecursive(node dst.Node) bool

HasAnnotationRecursive determines whether the given node or one of its children has a golines annotation on it. It's currently implemented for function declarations, fields, call expressions, and selector expressions only.

func HasMultiKeyTags

func HasMultiKeyTags(lines []string) bool

HasMultiKeyTags returns whether the given lines have a multikey struct line. It's used as an optimization step to avoid unnnecessary shortening rounds.

func HasTailAnnotation

func HasTailAnnotation(node dst.Node) bool

HasTailAnnotation determines whether the given AST node has a line length annotation at its end. This is needed to catch long function declarations with inline interface definitions.

func IsAnnotation

func IsAnnotation(line string) bool

IsAnnotation determines whether the given line is an annotation created with CreateAnnotation.

func ParseAnnotation

func ParseAnnotation(line string) int

ParseAnnotation returns the line length encoded in a golines annotation. If none is found, it returns -1.

Types

type Shortener

type Shortener struct {
	// contains filtered or unexported fields
}

Shortener shortens a single go file according to a small set of user style preferences.

func NewShortener

func NewShortener(config ShortenerConfig) *Shortener

NewShortener creates a new shortener instance from the provided config.

func (*Shortener) Shorten

func (s *Shortener) Shorten(contents []byte) ([]byte, error)

Shorten shortens the provided golang file content bytes.

type ShortenerConfig

type ShortenerConfig struct {
	MaxLen          int    // Max target width for each line
	TabLen          int    // Width of a tab character
	KeepAnnotations bool   // Whether to keep annotations in final result (for debugging only)
	ShortenComments bool   // Whether to shorten comments
	ReformatTags    bool   // Whether to reformat struct tags in addition to shortening long lines
	IgnoreGenerated bool   // Whether to ignore generated files
	DotFile         string // Path to write dot-formatted output to (for debugging only)
	ChainSplitDots  bool   // Whether to split chain methods by putting dots at ends of lines

	// Formatter that will be run before and after main shortening process. If empty,
	// defaults to goimports (if found), otherwise gofmt.
	BaseFormatterCmd string
}

ShortenerConfig stores the configuration options exposed by a Shortener instance.

Directories

Path Synopsis
Package fixtures contains made-up go files used for testing golines under various conditions.
Package fixtures contains made-up go files used for testing golines under various conditions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL