Documentation ¶
Overview ¶
Package autoimport tries to find which import should be used, given the start of a class name
Index ¶
- Variables
- func DeGlob(imports string) []string
- func FindJava() (string, error)
- func FindKotlin() (string, error)
- func Fix(filename string, removeExistingImports, deGlob, verbose bool) ([]byte, error)
- func ForEachByteLine(filename string, process func([]byte)) error
- func ForEachByteLineInData(data []byte, process func([]byte))
- func ForEachLine(filename string, process func(string, string)) error
- func ForEachLineInData(data []byte, process func(string, string))
- type ImportMatcher
- func (ima *ImportMatcher) ClassMap() map[string]string
- func (ima *ImportMatcher) FileImports(filename string, verbose bool) (string, error)
- func (impM *ImportMatcher) FindImports(sourceCode string) []string
- func (ima *ImportMatcher) FixImports(data []byte, verbose bool) ([]byte, error)
- func (ima *ImportMatcher) ImportBlock(data []byte, verbose bool) ([]byte, error)
- func (ima *ImportMatcher) ImportPathExact(exactClassName string) string
- func (impM *ImportMatcher) OrganizedImports(sourceCode string, onlyJava bool) string
- func (ima *ImportMatcher) StarPath(startOfClassName string) (string, string)
- func (ima *ImportMatcher) StarPathAll(startOfClassName string) ([]string, []string)
- func (ima *ImportMatcher) StarPathAllExact(exactClassName string) ([]string, []string)
- func (ima *ImportMatcher) StarPathExact(exactClassName string) string
- func (ima *ImportMatcher) String() string
Constants ¶
This section is empty.
Variables ¶
var KotlinTypes = []string{
"Annotation", "Any", "Array", "Boolean", "Byte", "Char", "CharSequence",
"Collection", "Comparable", "Double", "Enum", "Float", "Function", "Int",
"IntrinsicConstEvaluation", "Iterable", "Iterator", "List", "ListIterator",
"Long", "Map", "Map.Entry", "MutableCollection", "MutableIterable",
"MutableIterator", "MutableList", "MutableListIterator", "MutableMap",
"MutableMap.MutableEntry", "MutableSet", "Nothing", "Number", "Pair",
"PlatformDependent", "PureReifiable", "Runnable", "Set", "Short", "String",
"Throwable", "Triple", "Unit", "UByte", "UInt", "ULong", "UShort",
}
KotlinTypes lists classes and types that are available in Kotlin (with imports in mind)
Functions ¶
func DeGlob ¶ added in v1.4.4
DeGlob takes a string like "import java.util.*; // ArrayList" and returns "import java.util.ArrayList", for each class/type name that is listed as a comma separated list after "//".
func FindJava ¶
FindJava finds the most likely location of a Java installation (with subfolders with .jar files) on the system.
func FindKotlin ¶
FindKotlin finds the most likely location of a Kotlin installation (with subfolders with .jar files) on the system.
func Fix ¶ added in v1.5.0
Fix reads in a file and tries to organize the imports. removeExistingImports can be set to true to remove existing imports deGlob can be set to true to try to expand wildcard imports
func ForEachByteLine ¶ added in v1.1.0
ForEachByteLine splits a file on '\n' and iterates over the byte slices
func ForEachByteLineInData ¶ added in v1.3.0
ForEachByteLineInData splits data on '\n' and iterates over the byte slices
func ForEachLine ¶ added in v1.1.0
ForEachLine splits a file on '\n' and iterates over the lines. The callback function will be given each line and trimmed line as the function iterates.
func ForEachLineInData ¶ added in v1.3.0
ForEachLineInData splits data on '\n' and iterates over the lines. The callback function will be given each line and trimmed line as the function iterates.
Types ¶
type ImportMatcher ¶
type ImportMatcher struct { JARPaths []string // list of paths to examine for .jar files DeGlob bool // generate import statements without "*" // contains filtered or unexported fields }
ImportMatcher is a struct that contains a list of JAR file paths, and a lookup map from class names to class paths, which is populated when New or NewCustom is called.
func New ¶
func New(settings ...bool) (*ImportMatcher, error)
New creates a new ImportMatcher. If onlyJava is false, /usr/share/kotlin/lib will be added to the .jar file search path. The first (optional) bool should be set to true if only Java should be considered, and not Kotlin. The second (optional) bool should be set to true if the import organizer should always start out with removing existing imports. The third (optional) bool should be set to true if the generated imports should be exact intead of with a glob ("*").
func NewCustom ¶
func NewCustom(JARPaths []string, settings ...bool) (*ImportMatcher, error)
NewCustom creates a new ImportMatcher, given a slice of paths to search for .jar files The first (optional) bool should be set to true if only Java should be considered, and not Kotlin. The second (optional) bool should be set to true if the import organizer should always start out with removing existing imports. The third (optional) bool should be set to true if the generated imports should be exact intead of with a glob ("*").
func (*ImportMatcher) ClassMap ¶
func (ima *ImportMatcher) ClassMap() map[string]string
ClassMap returns the mapping from class names to class paths
func (*ImportMatcher) FileImports ¶ added in v1.1.0
func (ima *ImportMatcher) FileImports(filename string, verbose bool) (string, error)
FileImports generates sorted "import" lines for a .java or .kotlin file (the ImportMatcher should be configured to be either for Java or Kotlin as well)
func (*ImportMatcher) FindImports ¶
func (impM *ImportMatcher) FindImports(sourceCode string) []string
FindImports can find words that looks like classes, and then look up appropriate import package paths. Ignores "java.lang." classes.
func (*ImportMatcher) FixImports ¶ added in v1.3.0
func (ima *ImportMatcher) FixImports(data []byte, verbose bool) ([]byte, error)
FixImports generates sorted "import" lines for a .java or .kotlin file (the ImportMatcher should be configured to be either for Java or Kotlin as well). The existing imports (if any) are the replaced with the generated imports.
func (*ImportMatcher) ImportBlock ¶ added in v1.4.0
func (ima *ImportMatcher) ImportBlock(data []byte, verbose bool) ([]byte, error)
ImportBlock generates "import" lines for the given Java or Kotlin source code
func (*ImportMatcher) ImportPathExact ¶
func (ima *ImportMatcher) ImportPathExact(exactClassName string) string
ImportPathExact takes the exact class name and tries to return the shortest specific import path for the matching class. For example, "File" could result in "java.io.File". The function returns an empty string if there are no matches.
func (*ImportMatcher) OrganizedImports ¶
func (impM *ImportMatcher) OrganizedImports(sourceCode string, onlyJava bool) string
OrganizedImports generates import statements for packages that belongs to classes that are found in the given source code. If onlyJava is true, a semicolon is added after each line, and Kotlin jar files are not considered.
func (*ImportMatcher) StarPath ¶
func (ima *ImportMatcher) StarPath(startOfClassName string) (string, string)
StarPath takes the start of the class name and tries to return the shortest found class name, and also the import path like "java.io.*" Returns empty strings if there are no matches.
func (*ImportMatcher) StarPathAll ¶
func (ima *ImportMatcher) StarPathAll(startOfClassName string) ([]string, []string)
StarPathAll takes the start of the class name and tries to return all found class names, and also the import paths, like "java.io.*". Returns empty strings if there are no matches.
func (*ImportMatcher) StarPathAllExact ¶
func (ima *ImportMatcher) StarPathAllExact(exactClassName string) ([]string, []string)
StarPathAllExact takes the exact class name and tries to return all matching class names, and also the import paths, like "java.io.*". Returns empty strings if there are no matches.
func (*ImportMatcher) StarPathExact ¶
func (ima *ImportMatcher) StarPathExact(exactClassName string) string
StarPathExact takes the exact class name and tries to return the shortest import path for the matching class, if found, like "java.io.*". Returns empty string if there are no matches.
func (*ImportMatcher) String ¶
func (ima *ImportMatcher) String() string