DScintilla,代码折叠对我不起作用
我有这段代码,用 xml 词法分析器初始化 Scintilla:
procedure TfrmWeMain.DScintilla1MarginClick(ASender: TObject; AModifiers,
APosition, AMargin: Integer);
var line_number:integer;
begin
line_number:= (ASender as TDScintilla).SendEditor(SCI_LINEFROMPOSITION, APosition, 0);
case AMargin of
1:
begin
(ASender as TDScintilla).SendEditor(SCI_TOGGLEFOLD, line_number, 0);
end
end;
end;
procedure TfrmWeMain.addDocument(filename:string);
var frmEditor:tFrameEditor;
ts:TTabSheet;
procedure setColors(lang:integer;fore:integer;const back:tcolor=clWindow;
const bold:boolean=false; const italic:boolean=false;
const underline:boolean=false; const font:string='Courier New';
const size:integer=10);
begin
frmEditor.sci.StyleSetBack(lang,colortorgb(back));
frmEditor.sci.StyleSetFore(lang,colortorgb(fore));
frmEditor.sci.StyleSetFont(lang,font);
frmEditor.sci.StyleSetBold(lang,bold);
frmEditor.sci.StyleSetItalic(lang,italic);
frmEditor.sci.StyleSetUnderline(lang,underline);
frmEditor.sci.StyleSetSize(lang,size);
end;
procedure setFolding;
begin
frmEditor.sci.SetMarginTypeN(1,0);
frmEditor.sci.SetMarginTypeN(1,SC_MARGIN_SYMBOL);
frmEditor.sci.SetMarginMaskN(1,SC_MASK_FOLDERS);
frmEditor.sci.SetMarginWidthN(0,40);
frmEditor.sci.SetMarginWidthN(1,20);
frmEditor.sci.SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDER, SC_MARK_PLUS);
frmEditor.sci.SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDEROPEN, SC_MARK_MINUS);
frmEditor.sci.SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDEREND, SC_MARK_EMPTY);
frmEditor.sci.SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDERMIDTAIL, SC_MARK_EMPTY);
frmEditor.sci.SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDEROPENMID, SC_MARK_EMPTY);
frmEditor.sci.SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDERSUB, SC_MARK_EMPTY);
frmEditor.sci.SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDERTAIL, SC_MARK_EMPTY);
frmEditor.sci.SendEditor(SCI_SETFOLDFLAGS, 16, 0); // 16 Draw line below if not expanded
frmEditor.sci.OnMarginClick:=DScintilla1MarginClick;
frmeditor.sci.StartStyling(0,0);
end;
begin
///...
frmEditor.sci.SetLexer(SCLEX_XML);
frmEditor.sci.SetCodePage(CP_UTF8);
setColors(SCE_H_DEFAULT,clBlack);
setColors(SCE_H_TAG,clPurple,clWindow,true);
setColors(SCE_H_TAGUNKNOWN,clRed);
setColors(SCE_H_ATTRIBUTE,clNavy);
setColors(SCE_H_ATTRIBUTEUNKNOWN,clRed);
setColors(SCE_H_NUMBER,clBlue);
setColors(SCE_H_DOUBLESTRING,clBlue);
setColors(SCE_H_SINGLESTRING,clBlue);
setColors(SCE_H_OTHER,clBlack);
setColors(SCE_H_COMMENT,clTeal);
setColors(SCE_H_ENTITY,clPurple);
setColors(SCE_H_TAGEND,clPurple);
setColors(SCE_H_CDATA,clTeal);
setFolding;
///...
end;
请参阅代码的 setFolding 部分,这是折叠应该开始运动的地方,但事实并非如此。我看不到折叠标记,折叠本身也不起作用。
我需要知道我在这里错过了什么,或者我做错了什么。欢迎使用 Delphi、C++、C# 或伪代码的代码片段
I have this code, to initialize Scintilla with xml lexer:
procedure TfrmWeMain.DScintilla1MarginClick(ASender: TObject; AModifiers,
APosition, AMargin: Integer);
var line_number:integer;
begin
line_number:= (ASender as TDScintilla).SendEditor(SCI_LINEFROMPOSITION, APosition, 0);
case AMargin of
1:
begin
(ASender as TDScintilla).SendEditor(SCI_TOGGLEFOLD, line_number, 0);
end
end;
end;
procedure TfrmWeMain.addDocument(filename:string);
var frmEditor:tFrameEditor;
ts:TTabSheet;
procedure setColors(lang:integer;fore:integer;const back:tcolor=clWindow;
const bold:boolean=false; const italic:boolean=false;
const underline:boolean=false; const font:string='Courier New';
const size:integer=10);
begin
frmEditor.sci.StyleSetBack(lang,colortorgb(back));
frmEditor.sci.StyleSetFore(lang,colortorgb(fore));
frmEditor.sci.StyleSetFont(lang,font);
frmEditor.sci.StyleSetBold(lang,bold);
frmEditor.sci.StyleSetItalic(lang,italic);
frmEditor.sci.StyleSetUnderline(lang,underline);
frmEditor.sci.StyleSetSize(lang,size);
end;
procedure setFolding;
begin
frmEditor.sci.SetMarginTypeN(1,0);
frmEditor.sci.SetMarginTypeN(1,SC_MARGIN_SYMBOL);
frmEditor.sci.SetMarginMaskN(1,SC_MASK_FOLDERS);
frmEditor.sci.SetMarginWidthN(0,40);
frmEditor.sci.SetMarginWidthN(1,20);
frmEditor.sci.SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDER, SC_MARK_PLUS);
frmEditor.sci.SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDEROPEN, SC_MARK_MINUS);
frmEditor.sci.SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDEREND, SC_MARK_EMPTY);
frmEditor.sci.SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDERMIDTAIL, SC_MARK_EMPTY);
frmEditor.sci.SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDEROPENMID, SC_MARK_EMPTY);
frmEditor.sci.SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDERSUB, SC_MARK_EMPTY);
frmEditor.sci.SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDERTAIL, SC_MARK_EMPTY);
frmEditor.sci.SendEditor(SCI_SETFOLDFLAGS, 16, 0); // 16 Draw line below if not expanded
frmEditor.sci.OnMarginClick:=DScintilla1MarginClick;
frmeditor.sci.StartStyling(0,0);
end;
begin
///...
frmEditor.sci.SetLexer(SCLEX_XML);
frmEditor.sci.SetCodePage(CP_UTF8);
setColors(SCE_H_DEFAULT,clBlack);
setColors(SCE_H_TAG,clPurple,clWindow,true);
setColors(SCE_H_TAGUNKNOWN,clRed);
setColors(SCE_H_ATTRIBUTE,clNavy);
setColors(SCE_H_ATTRIBUTEUNKNOWN,clRed);
setColors(SCE_H_NUMBER,clBlue);
setColors(SCE_H_DOUBLESTRING,clBlue);
setColors(SCE_H_SINGLESTRING,clBlue);
setColors(SCE_H_OTHER,clBlack);
setColors(SCE_H_COMMENT,clTeal);
setColors(SCE_H_ENTITY,clPurple);
setColors(SCE_H_TAGEND,clPurple);
setColors(SCE_H_CDATA,clTeal);
setFolding;
///...
end;
See the setFolding part of the code, it's that where the folding should get in motion, but it doesn't. I can't see the fold markers, neither the folding itself works.
I need to know what i am missing here, or what i am doing wrong. Code snippets in Delphi, C++, C# or pseudocode are welcome
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有 XML 的折叠示例。您已经非常接近让它工作了,但是您错过了设置 2 个重要的细节,启用全局折叠和您的语言(请参阅 here for Fold.html XML 折叠属性),然后使折叠边距对鼠标单击敏感,这实际上使 OnMarginClick 事件能够被触发(参见 此处)。
在此示例中,我避免使用 SendEditor 消息传递并使用了 DScintilla 包装器的全部功能。
希望这有帮助:)
Here you have the folding example for XML. You were quite close to get it to work, but you missed to set 2 important details, enable folding globally and for your language (see here for fold.html folding property for XML) and after that enable fold margin to be sensitive for mouse clicks, what actually enables the OnMarginClick event to be fired (see here).
For this example I've avoided to use the SendEditor messaging and used the full power of the DScintilla wrapper.
Hope this helps :)