编辑器

添加js文件语法支持
This commit is contained in:
JianjunLiu 2023-03-07 14:52:43 +08:00
parent dc98618d53
commit 029ead93cd
3 changed files with 271 additions and 110 deletions

View File

@ -1831,6 +1831,7 @@ type TEditer=class(TCustomcontrol) //
FSynClasses["txt"]:= array(class(TSynHighLighter),class(TSynCompletion),";txt;");
FSynClasses["tsl"]:= array(class(TTslSynHighLighter),class(TTslCompletion),";tsl;tsf;pas;stm;");
FSynClasses["json"]:= array(class(TJsonSynHighLighter),class(TSynCompletion),";json;");
FSynClasses["js"]:= array(class(TJsSynHighLighter),class(tjssyncompletion),";js;");
FSynClasses["ini"]:= array(class(TINISynHigLighter),class(TSynCompletion),";ini;");
FSynClasses["bat"]:= array(class(TBatSynHigLighter),class(TSynCompletion),";bat;cmd;");
FSynClasses["tfm"]:= array(class(ttfmhighlighter),class(TSynCompletion),";tfm;");

View File

@ -129,6 +129,54 @@ type thighlitcolor = class(tcomponent)
end
FColors;
end
type tjssyncompletion = class(TSynCompletion)
function Create(AOwner);
begin
inherited;
IgnoreCase := false;
end
function PrepareCompletion(m);override;
begin
//½âÎö
if not Memo then return ;
sd := static getjswords();
d := gettextwords(Memo.Text);
if d then sd union=d;
SetCompData(sd);
end
function getjswords();
begin
getjskeywordstip(r);
getcsskeywords(r);
return r;
end
function gettextwords(s);
begin
parseregexpr("[A-Zaz0-9_\\-]+",s,"mi",mched,mchpos,mathlen);
r := array();
for ri,v in mched do
begin
v0 := v[0];
lv := lowercase(v0);
if r[lv] then continue;
r[lv] := v0;
end
d := array();
i := 0;
for lv,v in r do
begin
d[i,"caption"] := v;
d[i,"value"] := v;
d[i,"lvalue"] := lv;
cl := length(v);
d[i,"clen"] := cl;
d[i,"vlen"] := cl;
i++;
end
return d;
end
end
type TTSLCompletion= class(TSynCompletion)
{**
@explan(˵Ã÷) tslÌáʾ×Ô¶¯Íê³ÉÀà
@ -242,107 +290,9 @@ type TTSLCompletion= class(TSynCompletion)
r[idx]["order"] := 2;
idx++;
end
//cfk "abstract","break","case","class","continue","delete","do","else","eval","false","goto","if","new","while","with","for","function","in","interface","finally","private","protected","public","return","static","true","try","var",
for i,v in array("arguments","boolean","byte","catch","char","const","debugger","default","double","enum","export","extends","final","float","implements","import","instanceof","int","let","long","native","null","package","short","super","switch","synchronized","this","typeof","throw","throws","transient","void","volatile","yield")
do
begin
c := v+" <js key>";
r[idx]["caption"] := c;
r[idx]["value"] := v;
r[idx]["lvalue"] := lowercase(v);
r[idx]["vlen"] := length(v);
r[idx]["clen"] := length(c);
r[idx]["order"] := 4;
idx++;
getjskeywordstip(r);
getcsskeywords(r);
end
//"Array","function","length","eval",
for i,v in array("Date","hasOwnProperty","Infinity","isFinite","isNaN","isPrototypeOf","Math",
"NaN","name","Number","Object","prototype","String","toString","undefined","valueOf")
do
begin
c := v+" <js class>";
r[idx]["caption"] := c;
r[idx]["value"] := v;
r[idx]["lvalue"] := lowercase(v);
r[idx]["vlen"] := length(v);
r[idx]["clen"] := length(c);
r[idx]["order"] := 4;
idx++;
end
//"all","select","self",
for i,v in array("$","alert","anchor","anchors","area","assign","blur","button","checkbox",
"clearInterval","clearTimeout","clientInformation","close","closed","confirm",
"constructor","crypto","decodeURI","decodeURIComponent","defaultStatus","document",
"element","elements","embed","embeds","encodeURI","encodeURIComponent","escape","event",
"fileUpload","focus","form","forms","frame","innerHeight","innerWidth","layer","layers",
"link","location","mimeTypes","navigate","navigator","frames","frameRate","hidden","history",
"image","images","offscreenBuffering","open","opener","option","outerHeight","outerWidth","packages",
"pageXOffset","pageYOffset","parent","parseFloat","parseInt","password","pkcs11","plugin","prompt",
"propertyIsEnum","radio","reset","screenX","screenY","scroll","secure","setInterval",
"setTimeout","status","submit","taint","text","textarea","top","unescape","untaint","window")
do
begin
c := v+" <js win>";
r[idx]["caption"] := c;
r[idx]["value"] := v;
r[idx]["lvalue"] := lowercase(v);
r[idx]["vlen"] := length(v);
r[idx]["clen"] := length(c);
r[idx]["order"] := 4;
idx++;
end
for i,v in array("onblur","onclick","onerror","onfocus","onkeydown","onkeypress","onkeyup","onmouseover","onload","onmouseup","onmousedown","onsubmit")
do
begin
c := v+" <html ev>";
r[idx]["caption"] := c;
r[idx]["value"] := v;
r[idx]["lvalue"] := lowercase(v);
r[idx]["vlen"] := length(v);
r[idx]["clen"] := length(c);
r[idx]["order"] := 4;
idx++;
end
for i,v in array("align-content","align-items","align-self","all","animation","animation-delay",
"animation-direction","animation-duration","animation-fill-mode","animation-iteration-count",
"animation-name","animation-play-state","animation-timing-function","appearance","backface-visibility",
"background","background-attachment","background-blend-mode","background-clip","background-color",
"background-image","background-origin","background-position","background-repeat","background-size",
"border","border-bottom","border-bottom-color","border-bottom-left-radius","border-bottom-right-radius",
"border-bottom-style","border-bottom-width","border-collapse","border-color","border-image",
"border-image-outset","border-image-repeat","border-image-slice","border-image-source","border-image-width",
"border-left","border-left-color","border-left-style","border-left-width","border-radius","border-right",
"border-right-color","border-right-style","border-right-width","border-spacing","border-style","border-top",
"border-top-color","border-top-left-radius","border-top-right-radius","border-top-style","border-top-width",
"border-width","bottom","box-align","box-direction","box-flex","box-flex-group","box-lines","box-ordinal-group",
"box-orient","box-pack","box-shadow","box-sizing","caption-side","clear","clip","color","column-count","column-fill",
"column-gap","column-rule","column-rule-color","column-rule-style","column-rule-width","column-span","column-width",
"columns","content","counter-increment","counter-reset","cursor","direction","display","empty-cells","filter","flex",
"flex-basis","flex-direction","flex-flow","flex-grow","flex-shrink","flex-wrap","float","font","font-face","font-family",
"font-size","font-size-adjust","font-stretch","font-style","font-variant","font-weight","grid-columns","grid-rows",
"hanging-punctuation","height","icon","justify-content","keyframes","left","letter-spacing","line-height","list-style",
"list-style-image","list-style-position","list-style-type","margin","margin-bottom","margin-left","margin-right","margin-top",
"max-height","max-width","media","min-height","min-width","mix-blend-mode","object-fit","object-position","nav-down","nav-index",
"nav-left","nav-right","nav-up","opacity","order","outline","outline-color","outline-offset","outline-style","outline-width",
"overflow","overflow-x","overflow-y","padding","padding-bottom","padding-left","padding-right","padding-top","page-break-after",
"page-break-before","page-break-inside","perspective","perspective-origin","position","punctuation-trim","quotes","resize","right",
"rotation","tab-size","table-layout","target","target-name","target-new","target-position","text-align","text-align-last",
"text-decoration","text-decoration-color","text-decoration-line","text-decoration-style","text-indent","text-justify",
"text-outline","text-overflow","text-shadow","text-transform","text-wrap","top","transform","transform-origin","transform-style",
"transition","transition-delay","transition-duration","transition-property","transition-timing-function","unicode-bidi","vertical-align",
"visibility","white-space","width","word-break","word-spacing","word-wrap","z-index","writing-mode")
do
begin
c := v+" <css pro>";
r[idx]["caption"] := c;
r[idx]["value"] := v;
r[idx]["lvalue"] := lowercase(v);
r[idx]["vlen"] := length(v);
r[idx]["clen"] := length(c);
r[idx]["order"] := 5;
idx++;
end
return r;
end
private
@ -381,8 +331,7 @@ type TTslSynHighLighter = class(TSynHighLighter)
FBinFunc[v]:=v;
end
FJsKeyWords := array();
for i,v in array("abstract","arguments","boolean","break","byte","case","catch","char","class","const","continue","debugger","default","delete","do","double","else","enum","eval","export","extends","false","final","finally","float","for","function","goto","if","implements","import","in","instanceof","int","interface","let","long","native","new","null","package","private","protected","public","return","short","static","super","switch","synchronized","this","throw","throws","transient","true","try","typeof","var","void","volatile","while","with","yield")
do
for i,v in getjskeywords() do
begin
FJsKeyWords[v]:=v;
@ -1386,7 +1335,7 @@ type TJsonSynHighLighter = class(tcustomsynhighlighter)
function create(AOwner);
begin
inherited;
ExecuteCommand("string",array(('"',"\\")));
ExecuteCommand("string",array(('"',"\\"),("'","\\")));
ExecuteCommand("keywords",array("null","true","false"));
ExecuteCommand("rowannotes",array());
ExecuteCommand("blockannotes",array());
@ -1394,6 +1343,32 @@ type TJsonSynHighLighter = class(tcustomsynhighlighter)
ExecuteCommand("pairs",array(("{","}"),("[","]")));
end
end
type TJsSynHighLighter = class(tcustomsynhighlighter)
function create(AOwner);
begin
inherited;
keywordcolor := 0xff0000;
stringcolor := 0x968080;
annotationcolor := 0x8D9600;
ExecuteCommand("string",array(('"',"\\")));
ExecuteCommand("keywords",jskeywords1());
ExecuteCommand("rowannotes",array("//"));
ExecuteCommand("blockannotes",array(("/*","*/")));
ExecuteCommand("syms",array("{","}","[","]","<",">","(",")",":",",","=","?","+","-","*","/","."));
ExecuteCommand("pairs",array(("{","}"),("[","]"),("(",")")));
ExecuteCommand("sysfun",jskeywords2());
end
function jskeywords1();
begin
return getjskeywords() union2 getdomeventwords();
end
function jskeywords2();
begin
return getjsclasswords() union2 getjsfunwords();
end
end
type ttfmhighlighter = class(tcustomsynhighlighter)
@ -1411,6 +1386,132 @@ type ttfmhighlighter = class(tcustomsynhighlighter)
end
implementation
function getcsskeywords(r);
begin
if not ifarray(r) then r := array();
idx := length(r);
for i,v in array("align-content","align-items","align-self","all","animation","animation-delay",
"animation-direction","animation-duration","animation-fill-mode","animation-iteration-count",
"animation-name","animation-play-state","animation-timing-function","appearance","backface-visibility",
"background","background-attachment","background-blend-mode","background-clip","background-color",
"background-image","background-origin","background-position","background-repeat","background-size",
"border","border-bottom","border-bottom-color","border-bottom-left-radius","border-bottom-right-radius",
"border-bottom-style","border-bottom-width","border-collapse","border-color","border-image",
"border-image-outset","border-image-repeat","border-image-slice","border-image-source","border-image-width",
"border-left","border-left-color","border-left-style","border-left-width","border-radius","border-right",
"border-right-color","border-right-style","border-right-width","border-spacing","border-style","border-top",
"border-top-color","border-top-left-radius","border-top-right-radius","border-top-style","border-top-width",
"border-width","bottom","box-align","box-direction","box-flex","box-flex-group","box-lines","box-ordinal-group",
"box-orient","box-pack","box-shadow","box-sizing","caption-side","clear","clip","color","column-count","column-fill",
"column-gap","column-rule","column-rule-color","column-rule-style","column-rule-width","column-span","column-width",
"columns","content","counter-increment","counter-reset","cursor","direction","display","empty-cells","filter","flex",
"flex-basis","flex-direction","flex-flow","flex-grow","flex-shrink","flex-wrap","float","font","font-face","font-family",
"font-size","font-size-adjust","font-stretch","font-style","font-variant","font-weight","grid-columns","grid-rows",
"hanging-punctuation","height","icon","justify-content","keyframes","left","letter-spacing","line-height","list-style",
"list-style-image","list-style-position","list-style-type","margin","margin-bottom","margin-left","margin-right","margin-top",
"max-height","max-width","media","min-height","min-width","mix-blend-mode","object-fit","object-position","nav-down","nav-index",
"nav-left","nav-right","nav-up","opacity","order","outline","outline-color","outline-offset","outline-style","outline-width",
"overflow","overflow-x","overflow-y","padding","padding-bottom","padding-left","padding-right","padding-top","page-break-after",
"page-break-before","page-break-inside","perspective","perspective-origin","position","punctuation-trim","quotes","resize","right",
"rotation","tab-size","table-layout","target","target-name","target-new","target-position","text-align","text-align-last",
"text-decoration","text-decoration-color","text-decoration-line","text-decoration-style","text-indent","text-justify",
"text-outline","text-overflow","text-shadow","text-transform","text-wrap","top","transform","transform-origin","transform-style",
"transition","transition-delay","transition-duration","transition-property","transition-timing-function","unicode-bidi","vertical-align",
"visibility","white-space","width","word-break","word-spacing","word-wrap","z-index","writing-mode")
do
begin
c := v+" <css pro>";
r[idx]["caption"] := c;
r[idx]["value"] := v;
r[idx]["lvalue"] := lowercase(v);
r[idx]["vlen"] := length(v);
r[idx]["clen"] := length(c);
r[idx]["order"] := 5;
idx++;
end
end
function getjskeywords();
begin
return array("abstract","arguments","boolean","break","byte","case","catch","char","class","const","continue",
"debugger","default","delete","do","double","else","enum","eval","export","extends","false","final","finally",
"float","for","function","goto","if","implements","import","in","instanceof","int","interface","let","long",
"native","new","null","package","private","protected","public","return","short","static","super","switch",
"synchronized","this","throw","throws","transient","true","try","typeof","var","void","volatile","while","with","yield");
end
function getjsclasswords();
begin
return array("Date","hasOwnProperty","Infinity","isFinite","isNaN","isPrototypeOf","Math",
"NaN","name","Number","Object","prototype","String","toString","undefined","valueOf");
end
function getjsfunwords();
begin
return array("$","alert","anchor","anchors","area","assign","blur","button","checkbox",
"clearInterval","clearTimeout","clientInformation","close","closed","confirm",
"constructor","crypto","decodeURI","decodeURIComponent","defaultStatus","document",
"element","elements","embed","embeds","encodeURI","encodeURIComponent","escape","event",
"fileUpload","focus","form","forms","frame","innerHeight","innerWidth","layer","layers",
"link","location","mimeTypes","navigate","navigator","frames","frameRate","hidden","history",
"image","images","offscreenBuffering","open","opener","option","outerHeight","outerWidth","packages",
"pageXOffset","pageYOffset","parent","parseFloat","parseInt","password","pkcs11","plugin","prompt",
"propertyIsEnum","radio","reset","screenX","screenY","scroll","secure","setInterval",
"setTimeout","status","submit","taint","text","textarea","top","unescape","untaint","window");
end
function getdomeventwords();
begin
return array("onblur","onclick","onerror","onfocus","onkeydown","onkeypress","onkeyup","onmouseover","onload","onmouseup","onmousedown","onsubmit");
end
function getjskeywordstip(r);
begin
if not ifarray(r) then r := array();
idx := length(r);
for i,v in getjskeywords() do
begin
c := v+" <js key>";
r[idx]["caption"] := c;
r[idx]["value"] := v;
r[idx]["lvalue"] := lowercase(v);
r[idx]["vlen"] := length(v);
r[idx]["clen"] := length(c);
r[idx]["order"] := 4;
idx++;
end
//"Array","function","length","eval",
for i,v in getjsclasswords() do
begin
c := v+" <js class>";
r[idx]["caption"] := c;
r[idx]["value"] := v;
r[idx]["lvalue"] := lowercase(v);
r[idx]["vlen"] := length(v);
r[idx]["clen"] := length(c);
r[idx]["order"] := 4;
idx++;
end
//"all","select","self",
for i,v in getjsfunwords()do
begin
c := v+" <js win>";
r[idx]["caption"] := c;
r[idx]["value"] := v;
r[idx]["lvalue"] := lowercase(v);
r[idx]["vlen"] := length(v);
r[idx]["clen"] := length(c);
r[idx]["order"] := 4;
idx++;
end
for i,v in getdomeventwords() do
begin
c := v+" <html ev>";
r[idx]["caption"] := c;
r[idx]["value"] := v;
r[idx]["lvalue"] := lowercase(v);
r[idx]["vlen"] := length(v);
r[idx]["clen"] := length(c);
r[idx]["order"] := 4;
idx++;
end
end
type tcolor = class()
function create(c);
begin

View File

@ -638,6 +638,7 @@ type TCustomMemo = class(TCustomScrollControl,TCustomMemoCmd) //
@explan(说明) 带滚动条的编辑控件 %%
**}
private
FSymchars;
[weakref]fongutterclick;//点击
ftmemlockv;
fundoing; //清空unredo标记
@ -1014,6 +1015,7 @@ type TCustomMemo = class(TCustomScrollControl,TCustomMemoCmd) //
function Create(AOwner);override;
begin
inherited;
FSymchars := array();
ftmemlockv := new tcountkernel();
FGutterColor := rgb(228,228,228);
fcurrentLineColor := rgb(232,232,255);
@ -1296,6 +1298,26 @@ type TCustomMemo = class(TCustomScrollControl,TCustomMemoCmd) //
IncPaintLock();
try
case cmd of
"symchars":
begin
if ifstring(data) then
begin
FSymchars := array();
for i := 1 to length(data) do
begin
vi := data[i];
if vi= "\t" or vi="\r" or vi="\n" or vi="\b" then
begin
continue;
end
FSymchars[vi] := true;
end
return ;
end
r := "";
for i,v in FSymchars do r+=i;
return r;
end
"ecruningto":
begin
if Fecruningto <> data then
@ -1673,6 +1695,7 @@ type TCustomMemo = class(TCustomScrollControl,TCustomMemoCmd) //
begin
cx--;
vi := s[cx];
if FSymchars[vi] then continue;
ivi := ord(vi);
{if (ivi<=0x2f) or (ivi>122 and ivi<=127) then
begin
@ -1702,6 +1725,7 @@ type TCustomMemo = class(TCustomScrollControl,TCustomMemoCmd) //
while cx <= ls do
begin
vi := s[cx];
if FSymchars[vi] then continue;
ivi := ord(vi);
//if (ivi<=0x2f) or (ivi>122 and ivi<=127) then break;
if(ivi<48)or(ivi>57 and ivi<65)or(ivi>90 and ivi<95)or(ivi>95 and ivi<97)or(ivi>122 and ivi <= 127)then break;
@ -3114,8 +3138,10 @@ type tcustomsynhighlighter = class(TSynHighLighter)
fsymcolor := 0xa000a0;
fnumbercolor := 0x666666;
fkeywordcolor := 0x0000ff;
fsysfuncolor := 0xff0000;
fstringcolor := 0xff00ff;
fannotationcolor := 0xff0000;
//fannotationcolor := 0xff0000;
fannotationcolor := 0xaa3300;
fignorecase := false;//忽略大小写
FChangeDeal := true;
FTokens := array();
@ -3134,6 +3160,7 @@ type tcustomsynhighlighter = class(TSynHighLighter)
("[","]"),
("{","}"),
));
setsysfun(array());
clean();
end
function ExecuteCommand(cmd,pm);override;
@ -3164,6 +3191,10 @@ type tcustomsynhighlighter = class(TSynHighLighter)
begin
return setsyms(pm);
end
"sysfun":
begin
return setsysfun(pm);
end
"pairs":
begin
return setpairs(pm);
@ -3221,6 +3252,10 @@ type tcustomsynhighlighter = class(TSynHighLighter)
if ifkeywords(bttk) then
begin
d.FFColor := fkeywordcolor;
end else
if ifsysfun(bttk) then
begin
d.FFColor := fsysfuncolor ;
end
dopair(d,bttk);
end else
@ -3256,6 +3291,7 @@ type tcustomsynhighlighter = class(TSynHighLighter)
inherited;
end
property keywordcolor:color read fkeywordcolor write fkeywordcolor;
property sysfuncolor:color read fsysfuncolor write fsysfuncolor;
property stringcolor:color read fstringcolor write fstringcolor;
property annotationcolor:color read fannotationcolor write fannotationcolor;
property symcolor:color read fsymcolor write fsymcolor;
@ -3273,6 +3309,7 @@ type tcustomsynhighlighter = class(TSynHighLighter)
fblockstiresa,
fblockstiresb,
frowstires,
fsysfuntires,
fsymstires) do
begin
for j,vj in v do
@ -3352,7 +3389,7 @@ type tcustomsynhighlighter = class(TSynHighLighter)
SetTToken(tokens,ttk,idx-1);
bostr := ostr;
ncost := array("annote",bostr,fblockstiresb[bostr]);
SetTToken(tokens,ostr,oidx-1);
SetTToken(tokens,ostr,oidx-1,array("annote"));
idx := oidx-1;
return ParserTokenLines(s,idx+1,e,ncost,tokens);
end else
@ -3470,6 +3507,18 @@ type tcustomsynhighlighter = class(TSynHighLighter)
end
end
end
function setsysfun(d);//设置系统函数
begin
st := new TTire();
fsysfuntires := array(st);
for i,v in d do
begin
if ifstring(v) and v then
begin
st.add(v);
end
end
end
function setsyms(d);//设置符号
begin
st := new TTire();
@ -3543,6 +3592,14 @@ type tcustomsynhighlighter = class(TSynHighLighter)
return true;
end
end
function ifsysfun(s);//系统函数判断
begin
for i,v in fsysfuntires do
begin
if v.find(s,length(s),1,idx,ostr) and (ostr=s) then
return true;
end
end
function ifdefsym(s,l,idx,oidx,ostr,tidx); //符号判断
begin
return cyclefind(fsymstires,s,l,idx,oidx,ostr,tidx);
@ -3588,11 +3645,13 @@ type tcustomsynhighlighter = class(TSynHighLighter)
fstrstires;
fstrstires_zy;
fsymstires;
fsysfuntires;
frowstires;
fblockstiresa;
fblockstiresb;
/////
fkeywordcolor;
fsysfuncolor;
fstringcolor;
fannotationcolor;
fsymcolor;
@ -3762,9 +3821,9 @@ type TSynCustomMemo = class(TCustomMemo)
s := s.FStr;
for i:= b[1] to e[1]-1 do
begin
ivi := ord(s[i]);
if(ivi<48) or (ivi>57 and ivi<65) or (ivi>90 and ivi<95) or(ivi>95 and ivi<97) or(ivi>122 and ivi<=127) then continue;
r+=s[i];
ivi := ord(s[i]);
if(ivi<48) or (ivi>57 and ivi<65) or (ivi>90 and ivi<95) or(ivi>95 and ivi<97) or(ivi>122 and ivi<=127) then continue;
r+=s[i];
end
except
r := "";