-
Notifications
You must be signed in to change notification settings - Fork 73
Replace console module with liner #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,245 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
package console | ||
|
||
import "strings" | ||
|
||
// CompleteLine returns a set of candidate TSQL keywords to complete the current input line | ||
func CompleteLine(line string) []string { | ||
idx := strings.LastIndexAny(line, " ;") + 1 | ||
// we don't try to complete without a starting letter | ||
if idx == len(line) { | ||
return []string{} | ||
} | ||
prefix := strings.ToUpper(string(line[idx:])) | ||
left := 0 | ||
right := len(keywords) - 1 | ||
for left <= right { | ||
mid := (left + right) / 2 | ||
comp := 0 | ||
if len(keywords[mid]) >= len(prefix) { | ||
comp = strings.Compare(prefix, string(keywords[mid][:len(prefix)])) | ||
} else { | ||
comp = strings.Compare(prefix, keywords[mid]) | ||
} | ||
if comp < 0 { | ||
right = mid - 1 | ||
} else if comp > 0 { | ||
left = mid + 1 | ||
} else { | ||
// look up and down the list from mid and return the slice of matching words | ||
first := mid - 1 | ||
last := mid + 1 | ||
for first >= 0 && strings.HasPrefix(keywords[first], prefix) { | ||
first-- | ||
} | ||
for last < len(keywords) && strings.HasPrefix(keywords[last], prefix) { | ||
last++ | ||
} | ||
lines := make([]string, last-first-1) | ||
for i, w := range keywords[first+1 : last] { | ||
lines[i] = mergeLine(line, w, idx) | ||
} | ||
return lines | ||
} | ||
} | ||
return []string{} | ||
} | ||
|
||
// mergeline appends keyword to line starting at index idx | ||
// It matches the case of the current character in the line | ||
func mergeLine(line string, keyword string, idx int) string { | ||
upcase := line[idx] >= 'A' && line[idx] <= 'Z' | ||
b := strings.Builder{} | ||
b.Write([]byte(line[:idx])) | ||
if !upcase { | ||
b.WriteString(strings.ToLower(keyword)) | ||
} else { | ||
b.WriteString(keyword) | ||
} | ||
return b.String() | ||
} | ||
|
||
var keywords = []string{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eventually we could enable users to override the keyword list with their own file. We could also add auto complete for commands. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a common format for keywords is the plist file - would be interesting for them to be imported from there - both for our own reuse of work for VS Code as well as for users to import their own. |
||
"ADD", | ||
"ALL", | ||
"ALTER", | ||
"AND", | ||
"ANY", | ||
"AS", | ||
"ASC", | ||
"AUTHORIZATION", | ||
"BACKUP", | ||
"BEGIN", | ||
"BETWEEN", | ||
"BREAK", | ||
"BROWSE", | ||
"BULK", | ||
"BY", | ||
"CASCADE", | ||
"CASE", | ||
"CHECK", | ||
"CHECKPOINT", | ||
"CLOSE", | ||
"CLUSTERED", | ||
"COALESCE", | ||
"COLLATE", | ||
"COLUMN", | ||
"COMMIT", | ||
"COMPUTE", | ||
"CONSTRAINT", | ||
"CONTAINS", | ||
"CONTAINSTABLE", | ||
"CONTINUE", | ||
"CONVERT", | ||
"CREATE", | ||
"CROSS", | ||
"CURRENT", | ||
"CURRENT_DATE", | ||
"CURRENT_TIME", | ||
"CURRENT_TIMESTAMP", | ||
"CURRENT_USER", | ||
"CURSOR", | ||
"DATABASE", | ||
"DBCC", | ||
"DEALLOCATE", | ||
"DECLARE", | ||
"DEFAULT", | ||
"DELETE", | ||
"DENY", | ||
"DESC", | ||
"DISTINCT", | ||
"DISTRIBUTED", | ||
"DOUBLE", | ||
"DROP", | ||
"ELSE", | ||
"END", | ||
"ERRLVL", | ||
"ESCAPE", | ||
"EXCEPT", | ||
"EXEC", | ||
"EXECUTE", | ||
"EXISTS", | ||
"EXIT", | ||
"EXTERNAL", | ||
"FETCH", | ||
"FILE", | ||
"FILLFACTOR", | ||
"FOR", | ||
"FOREIGN", | ||
"FREETEXT", | ||
"FREETEXTTABLE", | ||
"FROM", | ||
"FULL", | ||
"FUNCTION", | ||
"GOTO", | ||
"GRANT", | ||
"GROUP", | ||
"HAVING", | ||
"HOLDLOCK", | ||
"IDENTITY", | ||
"IDENTITY_INSERT", | ||
"IDENTITYCOL", | ||
"IF", | ||
"IN", | ||
"INDEX", | ||
"INNER", | ||
"INSERT", | ||
"INTERSECT", | ||
"INTO", | ||
"IS", | ||
"JOIN", | ||
"KEY", | ||
"KILL", | ||
"LEFT", | ||
"LIKE", | ||
"LINENO", | ||
"MERGE", | ||
"NATIONAL", | ||
"NOCHECK", | ||
"NONCLUSTERED", | ||
"NOT", | ||
"NULL", | ||
"NULLIF", | ||
"OF", | ||
"OFF", | ||
"OFFSETS", | ||
"ON", | ||
"OPEN", | ||
"OPENDATASOURCE", | ||
"OPENQUERY", | ||
"OPENROWSET", | ||
"OPENXML", | ||
"OPTION", | ||
"OR", | ||
"ORDER", | ||
"OUTER", | ||
"OVER", | ||
"PERCENT", | ||
"PIVOT", | ||
"PLAN", | ||
"PRIMARY", | ||
"PRINT", | ||
"PROC", | ||
"PROCEDURE", | ||
"PUBLIC", | ||
"RAISERROR", | ||
"READ", | ||
"READTEXT", | ||
"RECONFIGURE", | ||
"REFERENCES", | ||
"REPLICATION", | ||
"RESTORE", | ||
"RESTRICT", | ||
"RETURN", | ||
"REVERT", | ||
"REVOKE", | ||
"RIGHT", | ||
"ROLLBACK", | ||
"ROWCOUNT", | ||
"ROWGUIDCOL", | ||
"RULE", | ||
"SAVE", | ||
"SCHEMA", | ||
"SELECT", | ||
"SEMANTICKEYPHRASETABLE", | ||
"SEMANTICSIMILARITYDETAILSTABLE", | ||
"SEMANTICSIMILARITYTABLE", | ||
"SESSION_USER", | ||
"SET", | ||
"SETUSER", | ||
"SHUTDOWN", | ||
"SOME", | ||
"STATISTICS", | ||
"SYSTEM_USER", | ||
"TABLE", | ||
"TABLESAMPLE", | ||
"TEXTSIZE", | ||
"THEN", | ||
"TO", | ||
"TOP", | ||
"TRAN", | ||
"TRANSACTION", | ||
"TRIGGER", | ||
"TRUNCATE", | ||
"TRY_CONVERT", | ||
"TSEQUAL", | ||
"UNION", | ||
"UNIQUE", | ||
"UNPIVOT", | ||
"UPDATE", | ||
"UPDATETEXT", | ||
"USE", | ||
"USER", | ||
"VALUES", | ||
"VARYING", | ||
"VIEW", | ||
"WAITFOR", | ||
"WHEN", | ||
"WHERE", | ||
"WHERECURRENT", | ||
"WHILE", | ||
"WITH", | ||
"WRITETEXT", | ||
} |
Uh oh!
There was an error while loading. Please reload this page.