github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/shell/autocomplete/format.go (about)

     1  package autocomplete
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/lmorg/murex/utils/parser"
     7  )
     8  
     9  // FormatSuggestions applies some loose formatting rules to auto-completion
    10  // suggestions
    11  func FormatSuggestions(act *AutoCompleteT) {
    12  	//sortCompletions(act.Items)
    13  	formatSuggestionsArray(act.ParsedTokens, act.Items)
    14  	formatSuggestionsMap(act.ParsedTokens, &act.Definitions)
    15  }
    16  
    17  func formatSuggestionsArray(pt parser.ParsedTokens, items []string) {
    18  	for i := range items {
    19  		if len(items[i]) == 0 {
    20  			items[i] = " "
    21  			continue
    22  		}
    23  
    24  		if !pt.QuoteSingle && !pt.QuoteDouble && pt.QuoteBrace == 0 {
    25  			items[i] = strings.Replace(items[i], `\`, `\\`, -1)
    26  			items[i] = strings.Replace(items[i], ` `, `\ `, -1)
    27  			items[i] = strings.Replace(items[i], `'`, `\'`, -1)
    28  			items[i] = strings.Replace(items[i], `"`, `\"`, -1)
    29  			items[i] = strings.Replace(items[i], `(`, `\(`, -1)
    30  			items[i] = strings.Replace(items[i], `)`, `\)`, -1)
    31  			items[i] = strings.Replace(items[i], `{`, `\{`, -1)
    32  			items[i] = strings.Replace(items[i], `}`, `\}`, -1)
    33  			items[i] = strings.Replace(items[i], `;`, `\;`, -1)
    34  			items[i] = strings.Replace(items[i], `|`, `\|`, -1)
    35  			items[i] = strings.Replace(items[i], `?`, `\?`, -1)
    36  			items[i] = strings.Replace(items[i], `->`, `-\>`, -1)
    37  			items[i] = strings.Replace(items[i], `#`, `\#`, -1)
    38  
    39  			if items[i][len(items[i])-1] != ' ' &&
    40  				items[i][len(items[i])-1] != '=' &&
    41  				items[i][len(items[i])-1] != '/' &&
    42  				len(pt.VarSigil) == 0 {
    43  
    44  				items[i] += " "
    45  			}
    46  		}
    47  	}
    48  }
    49  
    50  func formatSuggestionsMap(pt parser.ParsedTokens, definitions *map[string]string) {
    51  	var (
    52  		newDef = make(map[string]string)
    53  		newKey string
    54  	)
    55  
    56  	for key, value := range *definitions {
    57  		if key == "" {
    58  			newDef[" "] = value
    59  			continue
    60  		}
    61  
    62  		newKey = key
    63  
    64  		if !pt.QuoteSingle && !pt.QuoteDouble && pt.QuoteBrace == 0 {
    65  			newKey = strings.Replace(newKey, `\`, `\\`, -1)
    66  			newKey = strings.Replace(newKey, ` `, `\ `, -1)
    67  			newKey = strings.Replace(newKey, `'`, `\'`, -1)
    68  			newKey = strings.Replace(newKey, `"`, `\"`, -1)
    69  			newKey = strings.Replace(newKey, `(`, `\(`, -1)
    70  			newKey = strings.Replace(newKey, `)`, `\)`, -1)
    71  			newKey = strings.Replace(newKey, `{`, `\{`, -1)
    72  			newKey = strings.Replace(newKey, `}`, `\}`, -1)
    73  			newKey = strings.Replace(newKey, `;`, `\;`, -1)
    74  			newKey = strings.Replace(newKey, `|`, `\|`, -1)
    75  			newKey = strings.Replace(newKey, `?`, `\?`, -1)
    76  			newKey = strings.Replace(newKey, `->`, `-\>`, -1)
    77  			newKey = strings.Replace(newKey, `#`, `\#`, -1)
    78  
    79  			if newKey[len(newKey)-1] != ' ' &&
    80  				newKey[len(newKey)-1] != '=' &&
    81  				newKey[len(newKey)-1] != '/' &&
    82  				len(pt.VarSigil) == 0 {
    83  
    84  				newKey += " "
    85  			}
    86  		}
    87  
    88  		newDef[newKey] = value
    89  	}
    90  
    91  	*definitions = newDef
    92  }