feat(shell): vscode ctrl k and f format code

This commit is contained in:
Alex Duan 2022-10-09 14:05:48 +08:00
parent 60d7ad82ac
commit b222ff4ba5
4 changed files with 874 additions and 1033 deletions

View File

@ -40,7 +40,6 @@ typedef struct StrName {
struct StrName* next;
} StrName;
typedef struct STire {
char type; // see define TIRE_
STireNode root;
@ -57,7 +56,6 @@ typedef struct SMatchNode {
struct SMatchNode* next;
} SMatchNode;
typedef struct SMatch {
SMatchNode* head;
SMatchNode* tail; // append node to tail
@ -65,7 +63,6 @@ typedef struct SMatch {
char pre[MAX_WORD_LEN];
} SMatch;
// ----------- interface -------------
// create prefix search tree, return value call freeTire to free

View File

@ -25,7 +25,6 @@
//
#define UNION_ALL " union all "
// extern function
void shellClearScreen(int32_t ecmd_pos, int32_t cursor_pos);
void shellGetPrevCharSize(const char* str, int32_t pos, int32_t* size, int32_t* width);
@ -33,7 +32,6 @@ void shellShowOnScreen(SShellCmd *cmd);
void shellInsertChar(SShellCmd* cmd, char* c, int size);
bool appendAfterSelect(TAOS* con, SShellCmd* cmd, char* p, int32_t len);
typedef struct SAutoPtr {
STire* p;
int ref;
@ -58,7 +56,9 @@ typedef struct {
} SWords;
SWords shellCommands[] = {
{"alter database <db_name> <alter_db_options> <anyword> <alter_db_options> <anyword> <alter_db_options> <anyword> <alter_db_options> <anyword> <alter_db_options> <anyword> ;", 0, 0, NULL},
{"alter database <db_name> <alter_db_options> <anyword> <alter_db_options> <anyword> <alter_db_options> <anyword> "
"<alter_db_options> <anyword> <alter_db_options> <anyword> ;",
0, 0, NULL},
{"alter dnode <dnode_id> balance ", 0, 0, NULL},
{"alter dnode <dnode_id> resetlog;", 0, 0, NULL},
{"alter dnode <dnode_id> debugFlag 141;", 0, 0, NULL},
@ -80,7 +80,10 @@ SWords shellCommands[] = {
{"alter user <user_name> <user_actions> <anyword> ;", 0, 0, NULL},
// 20
{"create table <anyword> using <stb_name> tags(", 0, 0, NULL},
{"create database <anyword> <db_options> <anyword> <db_options> <anyword> <db_options> <anyword> <db_options> <anyword> <db_options> <anyword> <db_options> <anyword> <db_options> <anyword> <db_options> <anyword> <db_options> <anyword> <db_options> <anyword> ;", 0, 0, NULL},
{"create database <anyword> <db_options> <anyword> <db_options> <anyword> <db_options> <anyword> <db_options> "
"<anyword> <db_options> <anyword> <db_options> <anyword> <db_options> <anyword> <db_options> <anyword> "
"<db_options> <anyword> <db_options> <anyword> ;",
0, 0, NULL},
{"create dnode ", 0, 0, NULL},
{"create index ", 0, 0, NULL},
{"create mnode on dnode <dnode_id> ;", 0, 0, NULL},
@ -169,126 +172,57 @@ SWords shellCommands[] = {
{"insert into <tb_name> file ", 0, 0, NULL},
{"trim database <db_name>", 0, 0, NULL},
{"use <db_name>", 0, 0, NULL},
{"quit", 0, 0, NULL}
};
{"quit", 0, 0, NULL}};
char* keywords[] = {
"and ",
"asc ",
"desc ",
"from ",
"fill(",
"limit ",
"where ",
"interval(",
"order by ",
"order by ",
"offset ",
"or ",
"group by ",
"now()",
"session(",
"sliding ",
"slimit ",
"soffset ",
"state_window(",
"today() ",
"union all select ",
"partition by "
};
"and ", "asc ", "desc ", "from ", "fill(", "limit ", "where ",
"interval(", "order by ", "order by ", "offset ", "or ", "group by ", "now()",
"session(", "sliding ", "slimit ", "soffset ", "state_window(", "today() ", "union all select ",
"partition by "};
char* functions[] = {
"count(",
"sum(",
"avg(",
"last(",
"last_row(",
"top(",
"interp(",
"max(",
"min(",
"now()",
"today()",
"percentile(",
"tail(",
"pow(",
"abs(",
"atan(",
"acos(",
"asin(",
"apercentile(",
"bottom(",
"cast(",
"ceil(",
"char_length(",
"cos(",
"concat(",
"concat_ws(",
"csum(",
"diff(",
"derivative(",
"elapsed(",
"first(",
"floor(",
"hyperloglog(",
"histogram(",
"irate(",
"leastsquares(",
"length(",
"log(",
"lower(",
"ltrim(",
"mavg(",
"mode(",
"tan(",
"round(",
"rtrim(",
"sample(",
"sin(",
"spread(",
"substr(",
"statecount(",
"stateduration(",
"stddev(",
"sqrt(",
"timediff(",
"timezone(",
"timetruncate(",
"twa(",
"to_unixtimestamp(",
"unique(",
"upper(",
"count(", "sum(",
"avg(", "last(",
"last_row(", "top(",
"interp(", "max(",
"min(", "now()",
"today()", "percentile(",
"tail(", "pow(",
"abs(", "atan(",
"acos(", "asin(",
"apercentile(", "bottom(",
"cast(", "ceil(",
"char_length(", "cos(",
"concat(", "concat_ws(",
"csum(", "diff(",
"derivative(", "elapsed(",
"first(", "floor(",
"hyperloglog(", "histogram(",
"irate(", "leastsquares(",
"length(", "log(",
"lower(", "ltrim(",
"mavg(", "mode(",
"tan(", "round(",
"rtrim(", "sample(",
"sin(", "spread(",
"substr(", "statecount(",
"stateduration(", "stddev(",
"sqrt(", "timediff(",
"timezone(", "timetruncate(",
"twa(", "to_unixtimestamp(",
"unique(", "upper(",
};
char* tb_actions[] = {
"add column ",
"modify column ",
"drop column ",
"rename column ",
"add tag ",
"modify tag ",
"drop tag ",
"rename tag ",
"set tag ",
"add column ", "modify column ", "drop column ", "rename column ", "add tag ",
"modify tag ", "drop tag ", "rename tag ", "set tag ",
};
char * user_actions[] = {
"pass ",
"enable ",
"sysinfo "
};
char* user_actions[] = {"pass ", "enable ", "sysinfo "};
char * tb_options[] = {
"comment ",
"watermark ",
"max_delay ",
"ttl ",
"rollup(",
"sma("
};
char* tb_options[] = {"comment ", "watermark ", "max_delay ", "ttl ", "rollup(", "sma("};
char * db_options[] = {
"keep ",
char* db_options[] = {"keep ",
"replica ",
"precision ",
"strict ",
@ -309,43 +243,22 @@ char * db_options[] = {
"wal_retention_period ",
"wal_roll_period ",
"wal_retention_size ",
"wal_segment_size "
};
"wal_segment_size "};
char * alter_db_options[] = {
"keep ",
"cachemodel ",
"cachesize ",
"wal_fsync_period ",
"wal_level "
};
char* alter_db_options[] = {"keep ", "cachemodel ", "cachesize ", "wal_fsync_period ", "wal_level "};
char * data_types[] = {
"timestamp",
"int",
"int unsigned",
"varchar(16)",
"float",
"double",
"binary(16)",
"nchar(16)",
"bigint",
"bigint unsigned",
"smallint",
"smallint unsigned",
"tinyint",
"tinyint unsigned",
"bool",
"json"
};
char* data_types[] = {"timestamp", "int",
"int unsigned", "varchar(16)",
"float", "double",
"binary(16)", "nchar(16)",
"bigint", "bigint unsigned",
"smallint", "smallint unsigned",
"tinyint", "tinyint unsigned",
"bool", "json"};
char * key_tags[] = {
"tags("
};
char* key_tags[] = {"tags("};
char * key_select[] = {
"select "
};
char* key_select[] = {"select "};
//
// ------- gobal variant define ---------
@ -356,7 +269,6 @@ int32_t curMatchIndex = -1; // current match shellCommands index
int32_t lastWordBytes = -1; // printShow last word length
bool waitAutoFill = false;
//
// ----------- global var array define -----------
//
@ -380,7 +292,6 @@ bool waitAutoFill = false;
#define WT_VAR_USERACTION 17
#define WT_VAR_KEYSELECT 18
#define WT_VAR_CNT 19
#define WT_FROM_DB_MAX 6 // max get content from db
@ -395,50 +306,23 @@ TdThreadMutex tiresMutex;
// save thread handle obtain var name from db server
TdThread* threads[WT_FROM_DB_CNT];
// obtain var name with sql from server
char varTypes[WT_VAR_CNT][64] = {
"<db_name>",
"<stb_name>",
"<tb_name>",
"<dnode_id>",
"<user_name>",
"<topic_name>",
"<stream_name>",
"<all_table>",
"<function>",
"<keyword>",
"<tb_actions>",
"<db_options>",
"<alter_db_options>",
"<data_types>",
"<key_tags>",
"<anyword>",
"<tb_options>",
"<user_actions>",
"<key_select>"
};
char varSqls[WT_FROM_DB_CNT][64] = {
"show databases;",
"show stables;",
"show tables;",
"show dnodes;",
"show users;",
"show topics;",
"show streams;"
};
char varTypes[WT_VAR_CNT][64] = {"<db_name>", "<stb_name>", "<tb_name>", "<dnode_id>", "<user_name>",
"<topic_name>", "<stream_name>", "<all_table>", "<function>", "<keyword>",
"<tb_actions>", "<db_options>", "<alter_db_options>", "<data_types>", "<key_tags>",
"<anyword>", "<tb_options>", "<user_actions>", "<key_select>"};
char varSqls[WT_FROM_DB_CNT][64] = {"show databases;", "show stables;", "show tables;", "show dnodes;",
"show users;", "show topics;", "show streams;"};
// var words current cursor, if user press any one key except tab, cursorVar can be reset to -1
int cursorVar = -1;
bool varMode = false; // enter var names list mode
TAOS* varCon = NULL;
SShellCmd* varCmd = NULL;
SMatch* lastMatch = NULL; // save last match result
int cntDel = 0; // delete byte count after next press tab
// show auto tab introduction
void printfIntroduction() {
printf(" **************************** How To Use TAB Key ********************************\n");
@ -459,7 +343,8 @@ void printfIntroduction() {
void showHelp() {
printf("\nThe following are supported commands for TDengine Command Line:");
printf("\n\
printf(
"\n\
----- A ----- \n\
alter database <db_name> <db_options> \n\
alter dnode <dnode_id> balance \n\
@ -574,7 +459,8 @@ void showHelp() {
printf("\n\n");
// define in getDuration() function
printf("\
printf(
"\
Timestamp expression Format:\n\
b - nanosecond \n\
u - microsecond \n\
@ -600,8 +486,7 @@ void showHelp() {
SWord* atWord(SWords* command, int32_t index) {
SWord* word = command->head;
for (int32_t i = 0; i < index; i++) {
if (word == NULL)
return NULL;
if (word == NULL) return NULL;
word = word->next;
}
@ -612,8 +497,7 @@ SWord * atWord(SWords * command, int32_t index) {
int wordType(const char* p, int32_t len) {
for (int i = 0; i < WT_VAR_CNT; i++) {
if (strncmp(p, varTypes[i], len) == 0)
return i;
if (strncmp(p, varTypes[i], len) == 0) return i;
}
return WT_TEXT;
}
@ -689,14 +573,12 @@ void freeCommand(SWords * command) {
SWord* tmp = word;
word = word->next;
// if malloc need free
if(tmp->free && tmp->word)
taosMemoryFree(tmp->word);
if (tmp->free && tmp->word) taosMemoryFree(tmp->word);
taosMemoryFree(tmp);
}
// if malloc need free
if(word->free && word->word)
taosMemoryFree(word->word);
if (word->free && word->word) taosMemoryFree(word->word);
taosMemoryFree(word);
}
@ -715,7 +597,6 @@ void GenerateVarType(int type, char** p, int count) {
// -------------------- shell auto ----------------
//
// init shell auto funciton , shell start call once
bool shellAutoInit() {
// command
@ -747,9 +628,7 @@ bool shellAutoInit() {
}
// set conn
void shellSetConn(TAOS* conn) {
varCon = conn;
}
void shellSetConn(TAOS* conn) { varCon = conn; }
// exit shell auto funciton, shell exit call once
void shellAutoExit() {
@ -790,8 +669,7 @@ void shellAutoExit() {
// ------------------- auto ptr for tires --------------------------
//
bool setNewAuotPtr(int type, STire* pNew) {
if (pNew == NULL)
return false;
if (pNew == NULL) return false;
taosThreadMutexLock(&tiresMutex);
STire* pOld = tires[type];
@ -846,8 +724,6 @@ void putBackAutoPtr(int type, STire* tire) {
return;
}
//
// ------------------- var Word --------------------------
//
@ -952,8 +828,7 @@ char* matchNextPrefix(STire* tire, char* pre) {
// save to lastMatch
if (match) {
if (lastMatch)
freeMatch(lastMatch);
if (lastMatch) freeMatch(lastMatch);
lastMatch = match;
}
}
@ -1011,8 +886,7 @@ char* tireSearchWord(int type, char* pre) {
if (type > WT_FROM_DB_MAX) {
// NOT FROM DB , tires[type] alwary not null
STire* tire = tires[type];
if (tire == NULL)
return NULL;
if (tire == NULL) return NULL;
return matchNextPrefix(tire, pre);
}
@ -1069,8 +943,7 @@ bool matchVarWord(SWord* word1, SWord* word2) {
str = tireSearchWord(WT_VAR_STABLE, pre);
if (str == NULL) {
str = tireSearchWord(WT_VAR_TABLE, pre);
if(str == NULL)
return false;
if (str == NULL) return false;
}
} else {
// OTHER
@ -1098,7 +971,6 @@ bool matchVarWord(SWord* word1, SWord* word2) {
// ------------------- match words --------------------------
//
// compare command cmd1 come from shellCommands , cmd2 come from user input
int32_t compareCommand(SWords* cmd1, SWords* cmd2) {
SWord* word1 = cmd1->head;
@ -1112,8 +984,7 @@ int32_t compareCommand(SWords * cmd1, SWords * cmd2) {
if (word1->type == WT_TEXT) {
// WT_TEXT match
if (word1->len == word2->len) {
if (strncasecmp(word1->word, word2->word, word1->len) != 0)
return -1;
if (strncasecmp(word1->word, word2->word, word1->len) != 0) return -1;
} else if (word1->len < word2->len) {
return -1;
} else {
@ -1172,8 +1043,7 @@ SWords * matchCommand(SWords * input, bool continueSearch) {
// compare
int32_t index = compareCommand(shellCommand, input);
if (index != -1) {
if (firstMatchIndex == -1)
firstMatchIndex = i;
if (firstMatchIndex == -1) firstMatchIndex = i;
curMatchIndex = i;
return &shellCommands[i];
}
@ -1226,8 +1096,7 @@ void printScreen(TAOS * con, SShellCmd * cmd, SWords * match) {
lastMatchIndex = firstMatchIndex;
lastWordBytes = word->len;
} else {
if (lastWordBytes == -1)
return ;
if (lastWordBytes == -1) return;
deleteCount(cmd, lastWordBytes);
SWord* word = MATCH_WORD(match);
@ -1242,7 +1111,6 @@ void printScreen(TAOS * con, SShellCmd * cmd, SWords * match) {
shellInsertChar(cmd, (char*)str, strLen);
}
// main key press tab , matched return true else false
bool firstMatchCommand(TAOS* con, SShellCmd* cmd) {
// parse command
@ -1319,8 +1187,7 @@ bool nextMatchCommand(TAOS * con, SShellCmd * cmd, SWords * firstMatch) {
match = matchCommand(input, false);
if (match == NULL) {
freeCommand(input);
if (input->source)
taosMemoryFree(input->source);
if (input->source) taosMemoryFree(input->source);
taosMemoryFree(input);
return false;
}
@ -1367,8 +1234,7 @@ bool fillTableName(TAOS * con, SShellCmd * cmd, char* pre) {
char* str = tireSearchWord(WT_VAR_STABLE, pre);
if (str == NULL) {
str = tireSearchWord(WT_VAR_TABLE, pre);
if(str == NULL)
return false;
if (str == NULL) return false;
}
// need insert part string
@ -1543,8 +1409,7 @@ int32_t searchAfterSelect(char* p, int32_t len) {
char* p1 = p + 7;
while (1) {
char* p2 = strstr(p1, "select ");
if(p2 == NULL)
break;
if (p2 == NULL) break;
p1 = p2 + 7;
}
@ -1557,13 +1422,13 @@ int32_t searchAfterSelect(char* p, int32_t len) {
}
char* as_pos_end = strstr(p, " as select ");
if (as_pos_end == NULL)
return -1;
if (as_pos_end == NULL) return -1;
as_pos_end += 11;
// create stream <stream_name> as select
if (strncasecmp(p, "create stream ", 14) == 0) {
return as_pos_end - p;;
return as_pos_end - p;
;
}
// create topic <topic_name> as select
@ -1600,8 +1465,7 @@ bool matchSelectQuery(TAOS * con, SShellCmd * cmd) {
char* sql_cp = strndup(p, len);
int32_t n = searchAfterSelect(sql_cp, len);
taosMemoryFree(sql_cp);
if(n == -1 || n > len)
return false;
if (n == -1 || n > len) return false;
p += n;
len -= n;
@ -1711,8 +1575,7 @@ bool matchOther(TAOS * con, SShellCmd * cmd) {
}
// too small
if(len < 8)
return false;
if (len < 8) return false;
// like 'from ( '
char* sql = strndup(p, len);
@ -1731,8 +1594,7 @@ bool matchOther(TAOS * con, SShellCmd * cmd) {
// find last ' from'
while (from) {
char* p1 = strstr(from + 5, " from");
if (p1 == NULL)
break;
if (p1 == NULL) break;
from = p1;
}
@ -1770,13 +1632,11 @@ bool matchOther(TAOS * con, SShellCmd * cmd) {
// INSERT
taosMemoryFree(sql);
return false;
}
// main key press tab
void pressTabKey(SShellCmd* cmd) {
// check
@ -1793,8 +1653,7 @@ void pressTabKey(SShellCmd * cmd) {
// manual match like create table st( ...
matched = matchCreateTable(varCon, cmd);
if (matched)
return ;
if (matched) return;
// shellCommands match
if (firstMatchIndex == -1) {
@ -1802,19 +1661,16 @@ void pressTabKey(SShellCmd * cmd) {
} else {
matched = nextMatchCommand(varCon, cmd, &shellCommands[firstMatchIndex]);
}
if (matched)
return ;
if (matched) return;
// NOT MATCHED ANYONE
// match other like '\G' ...
matched = matchOther(varCon, cmd);
if (matched)
return ;
if (matched) return;
// manual match like select * from ...
matched = matchSelectQuery(varCon, cmd);
if (matched)
return ;
if (matched) return;
return;
}
@ -2006,19 +1862,16 @@ bool dealDropCommand(char * sql) {
bool del = false;
// del in stable
STire* tire = tires[WT_VAR_STABLE];
if(tire)
del = deleteWord(tire, name);
if (tire) del = deleteWord(tire, name);
// del in table
if (!del) {
tire = tires[WT_VAR_TABLE];
if(tire)
del = deleteWord(tire, name);
if (tire) del = deleteWord(tire, name);
}
} else {
// OTHER TYPE
STire* tire = tires[type];
if(tire)
deleteWord(tire, name);
if (tire) deleteWord(tire, name);
}
taosThreadMutexUnlock(&tiresMutex);

View File

@ -15,8 +15,8 @@
#define __USE_XOPEN
#include "os.h"
#include "shellTire.h"
#include "os.h"
// ----------- interface -------------
@ -32,8 +32,7 @@ STire* createTire(char type) {
// free tire node
void freeTireNode(STireNode* node) {
if (node == NULL)
return ;
if (node == NULL) return;
// nest free sub node on array d
if (node->d) {
@ -184,8 +183,7 @@ bool deleteFromTree(STire* tire, char* word, int len) {
}
}
if(nodes[m]->d == NULL)
break;
if (nodes[m]->d == NULL) break;
// move to next node
nodes = nodes[m]->d;
}
@ -255,8 +253,7 @@ void enumAllWords(STireNode** nodes, char* prefix, SMatch* match) {
addWordToMatch(match, word);
}
// nested call next layer
if (c->d)
enumAllWords(c->d, word, match);
if (c->d) enumAllWords(c->d, word, match);
}
}
}
@ -310,12 +307,10 @@ void matchPrefixFromTree(STire* tire, char* prefix, SMatch* match) {
}
// prefix is match to end char
if (c->d)
enumAllWords(c->d, prefix, root);
if (c->d) enumAllWords(c->d, prefix, root);
} else {
// move to next node continue match
if(c->d == NULL)
break;
if (c->d == NULL) break;
nodes = c->d;
}
}
@ -348,7 +343,6 @@ SMatch* matchPrefix(STire* tire, char* prefix, SMatch* match) {
return match;
}
// get all items from tires tree
void enumFromList(STire* tire, SMatch* match) {
StrName* item = tire->head;
@ -410,16 +404,13 @@ SMatch* enumAll(STire* tire) {
return match;
}
// free match result
void freeMatchNode(SMatchNode* node) {
// first free next
if (node->next)
freeMatchNode(node->next);
if (node->next) freeMatchNode(node->next);
// second free self
if (node->word)
taosMemoryFree(node->word);
if (node->word) taosMemoryFree(node->word);
taosMemoryFree(node);
}