<commit_before>package completion

import (
	"flag"
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"
)

var completionLog = log.New(os.Stderr, "completion: ", log.LstdFlags)

type CommandLine []string
type Completer interface {
	Complete(CommandLine) []string
}
type FunctionCompleter func(CommandLine) []string

func (f FunctionCompleter) Complete(cl CommandLine) []string {
	return f(cl)
}

func (c CommandLine) CurrentWord() string {
	return c[len(c)-1]
}

func CompleteIfRequested(completer Completer) {
	if len(os.Args) <= 1 || os.Args[1] != "-do-completion" {
		return
	}
	line := os.Getenv("COMP_LINE")
	pointStr := os.Getenv("COMP_POINT")
	if line == "" || pointStr == "" {
		completionLog.Println("Completion requested, but COMP_LINE and/or COMP_POINT unset.")
		os.Exit(1)
	}

	point, err := strconv.ParseInt(pointStr, 10, 32)
	if err != nil {
		completionLog.Println("Invalid COMP_POINT: ", point)
		os.Exit(1)
	}

	cl := parseLineForCompletion(line, int(point))

	for _, word := range completer.Complete(cl) {
		fmt.Println(word)
	}
	os.Exit(0)
}

func parseLineForCompletion(line string, point int) CommandLine {
	var cl CommandLine
	var quote rune = 0
	var backslash bool = false
	var word []rune
	for _, char := range line[:point] {
		if backslash {
			word = append(word, char)
			backslash = false
			continue
		}
		if char == '\\' {
			word = append(word, char)
			backslash = true
			continue
		}

		switch quote {
		case 0:
			switch char {
			case '\'', '"':
				word = append(word, char)
				quote = char
			case ' ', '\t':
				if word != nil {
					cl = append(cl, string(word))
				}
				word = nil
			default:
				word = append(word, char)
			}
		case '\'':
			word = append(word, char)
			if char == '\'' {
				quote = 0
			}
		case '"':
			word = append(word, char)
			if char == '"' {
				quote = 0
			}
		}
	}

	return append(cl, string(word))
}

type boolFlag interface {
	flag.Value
	IsBoolFlag() bool
}

func completeFlags(cl CommandLine, flags *flag.FlagSet) (completions []string, rest CommandLine) {
	if len(cl) == 0 {
		return nil, cl
	}
	cl = cl[1:]
	var inFlag string
	for len(cl) > 1 {
		w := cl[0]
		if inFlag != "" {
			inFlag = ""
		} else if len(w) > 1 && w[0] == '-' && w != "--" {
			if !strings.Contains(w, "=") {
				var i int
				for i = 0; i < len(w) && w[i] == '-'; i++ {
				}
				inFlag = w[i:]
			}
			if flag := flags.Lookup(inFlag); flag != nil {
				if bf, ok := flag.Value.(boolFlag); ok && bf.IsBoolFlag() {
					inFlag = ""<commit_msg>completion: Drop the command from the CommandLine.<commit_after>package completion

import (
	"flag"
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"
)

var completionLog = log.New(os.Stderr, "completion: ", log.LstdFlags)

type CommandLine []string
type Completer interface {
	Complete(CommandLine) []string
}
type FunctionCompleter func(CommandLine) []string

func (f FunctionCompleter) Complete(cl CommandLine) []string {
	return f(cl)
}

func (c CommandLine) CurrentWord() string {
	return c[len(c)-1]
}

func CompleteIfRequested(completer Completer) {
	if len(os.Args) <= 1 || os.Args[1] != "-do-completion" {
		return
	}
	line := os.Getenv("COMP_LINE")
	pointStr := os.Getenv("COMP_POINT")
	if line == "" || pointStr == "" {
		completionLog.Println("Completion requested, but COMP_LINE and/or COMP_POINT unset.")
		os.Exit(1)
	}

	point, err := strconv.ParseInt(pointStr, 10, 32)
	if err != nil {
		completionLog.Println("Invalid COMP_POINT: ", point)
		os.Exit(1)
	}

	cl := parseLineForCompletion(line, int(point))[1:]

	for _, word := range completer.Complete(cl) {
		fmt.Println(word)
	}
	os.Exit(0)
}

func parseLineForCompletion(line string, point int) CommandLine {
	var cl CommandLine
	var quote rune = 0
	var backslash bool = false
	var word []rune
	for _, char := range line[:point] {
		if backslash {
			word = append(word, char)
			backslash = false
			continue
		}
		if char == '\\' {
			word = append(word, char)
			backslash = true
			continue
		}

		switch quote {
		case 0:
			switch char {
			case '\'', '"':
				word = append(word, char)
				quote = char
			case ' ', '\t':
				if word != nil {
					cl = append(cl, string(word))
				}
				word = nil
			default:
				word = append(word, char)
			}
		case '\'':
			word = append(word, char)
			if char == '\'' {
				quote = 0
			}
		case '"':
			word = append(word, char)
			if char == '"' {
				quote = 0
			}
		}
	}

	return append(cl, string(word))
}

type boolFlag interface {
	flag.Value
	IsBoolFlag() bool
}

func completeFlags(cl CommandLine, flags *flag.FlagSet) (completions []string, rest CommandLine) {
	if len(cl) == 0 {
		return nil, cl
	}
	var inFlag string
	for len(cl) > 1 {
		w := cl[0]
		if inFlag != "" {
			inFlag = ""
		} else if len(w) > 1 && w[0] == '-' && w != "--" {
			if !strings.Contains(w, "=") {
				var i int
				for i = 0; i < len(w) && w[i] == '-'; i++ {
				}
				inFlag = w[i:]
			}
			if flag := flags.Lookup(inFlag); flag != nil {
				if bf, ok := flag.Value.(boolFlag); ok && bf.IsBoolFlag() {
					inFlag = ""
{'fraction_non_alphanumeric': 0.16213514594162334, 'fraction_numerical': 0.008396641343462616, 'mean_word_length': 3.067479674796748, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6505131', 'n_tokens_mistral': 2117, 'n_tokens_neox': 1847, 'n_words': 478}