From aa4926084cd191d51513e5c14bdaed6504b8db10 Mon Sep 17 00:00:00 2001 From: cxl Date: Sat, 6 Dec 2008 12:24:04 +0000 Subject: [PATCH] theide - Complete abbreviation git-svn-id: svn://ultimatepp.org/upp/trunk@694 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/CodeEditor/CodeEditor.cpp | 4 +- uppsrc/CtrlLib/LineEdit.cpp | 2 +- uppsrc/CtrlLib/init | 2 +- uppsrc/ide/Abbr.cpp | 131 +++++++++++++++++++++++++++++++ uppsrc/ide/Assist.cpp | 41 ++++++++++ uppsrc/ide/ide.h | 6 ++ uppsrc/ide/ide.key | 3 +- uppsrc/ide/ide.lay | 11 +++ uppsrc/ide/ide.upp | 1 + uppsrc/ide/idebar.cpp | 3 + uppsrc/ide/idewin.cpp | 1 + 11 files changed, 200 insertions(+), 5 deletions(-) create mode 100644 uppsrc/ide/Abbr.cpp diff --git a/uppsrc/CodeEditor/CodeEditor.cpp b/uppsrc/CodeEditor/CodeEditor.cpp index 8934c2db6..0a142dcbd 100644 --- a/uppsrc/CodeEditor/CodeEditor.cpp +++ b/uppsrc/CodeEditor/CodeEditor.cpp @@ -989,7 +989,7 @@ void CodeEditor::PutI(WithDropChoice& edit) } CodeEditor::CodeEditor() { - bracket_flash = true; + bracket_flash = false; highlight_bracket_pos0 = 0; bracket_start = 0; stat_edit_time = 0; @@ -1016,7 +1016,7 @@ CodeEditor::CodeEditor() { bar.WhenAnnotationRightClick = Proxy(WhenAnnotationRightClick); highlight = HIGHLIGHT_NONE; hilite_scope = 0; - hilite_bracket = 2; + hilite_bracket = 1; hilite_ifdef = 1; indent_spaces = false; indent_amount = GetTabSize(); diff --git a/uppsrc/CtrlLib/LineEdit.cpp b/uppsrc/CtrlLib/LineEdit.cpp index f86ea0d50..d50f7773a 100644 --- a/uppsrc/CtrlLib/LineEdit.cpp +++ b/uppsrc/CtrlLib/LineEdit.cpp @@ -124,7 +124,7 @@ void LineEdit::Paint0(Draw& w) { int l = ngp - gp; LLOG("Highlight -> tab[" << q << "] paper = " << h.paper); w.DrawRect(gp * fsz.cx - scx, y, fsz.cx * l, fsz.cy, h.paper); - if(showtabs && h.paper != SColorHighlight) { + if(showtabs && h.paper != SColorHighlight && q < tx.GetLength()) { Color c = Blend(SColorLight, SColorHighlight); w.DrawRect(gp * fsz.cx - scx + 2, y + fsz.cy / 2, l * fsz.cx - 4, 1, c); diff --git a/uppsrc/CtrlLib/init b/uppsrc/CtrlLib/init index 1e10fe64d..88a0929e9 100644 --- a/uppsrc/CtrlLib/init +++ b/uppsrc/CtrlLib/init @@ -3,7 +3,7 @@ #include "CtrlCore/init" #include "RichText/init" #include "PdfDraw/init" -#define BLITZ_INDEX__ F2AFAB801F99B3E8069C425DA8E8DCF65 +#define BLITZ_INDEX__ F9E4BBC13A1F4208D11AB0446CDBF7C80 #include "CtrlLib.icpp" #undef BLITZ_INDEX__ #endif diff --git a/uppsrc/ide/Abbr.cpp b/uppsrc/ide/Abbr.cpp new file mode 100644 index 000000000..274159098 --- /dev/null +++ b/uppsrc/ide/Abbr.cpp @@ -0,0 +1,131 @@ +#include "ide.h" + +class AbbreviationsDlg : public WithAbbreviationsLayout { + typedef AbbreviationsDlg CLASSNAME; + EditString keyword; + + void Add(); + void Remove(); + void Edit(); + void Sync(); + void Menu(Bar& bar); + void Finish(const String& s); + +public: + AbbreviationsDlg(); +}; + +void AbbreviationsDlg::Sync() +{ + edit.Enable(abbr.IsCursor()); + remove.Enable(abbr.IsCursor()); +} + +void AbbreviationsDlg::Menu(Bar& bar) +{ + bar.Add("New..", THISBACK(Add)); + bar.Add(abbr.IsCursor(), "Edit..", THISBACK(Edit)); + bar.Add(abbr.IsCursor(), "Delete", THISBACK(Remove)); +} + +void AbbreviationsDlg::Finish(const String& s) +{ + abbr.Sort(); + Sync(); + abbr.FindSetCursor(s); +} + +void AbbreviationsDlg::Add() +{ + String s; + EditText(s, "New abbreviation", "Keyword", CharFilterAlpha); + abbr.Add(s, Null); + Finish(s); +} + +void AbbreviationsDlg::Remove() +{ + if(abbr.IsCursor() && PromptYesNo("Remove abbreviation?")) + abbr.Remove(abbr.GetCursor()); + Sync(); +} + +void AbbreviationsDlg::Edit() +{ + if(!abbr.IsCursor()) + return; + String s = abbr.GetKey(); + EditText(s, "Edit keyword", "Keyword", CharFilterAlpha); + abbr.Set(0, s); + Finish(s); +} + +AbbreviationsDlg::AbbreviationsDlg() +{ + CtrlLayoutOKCancel(*this, "Abbreviations"); + abbr.AddColumn("Keyword").Edit(keyword); + abbr.AddCtrl(code); + abbr.WhenBar = THISBACK(Menu); + add <<= THISBACK(Add); + remove <<= THISBACK(Remove); + edit <<= THISBACK(Edit); + code.Highlight(CodeEditor::HIGHLIGHT_CPP); + code.ShowTabs(); + Sync(); +} + +void Ide::Abbreviations() +{ + AbbreviationsDlg dlg; + for(int i = 0; i < abbr.GetCount(); i++) + dlg.abbr.Add(abbr.GetKey(i), abbr[i]); + if(dlg.Execute() != IDOK) + return; + abbr.Clear(); + for(int i = 0; i < dlg.abbr.GetCount(); i++) + abbr.Add(dlg.abbr.Get(i, 0), dlg.abbr.Get(i, 1)); + SaveAbbr(); +} + +void Ide::LoadAbbr() +{ + abbr.Clear(); + String s = LoadFile(ConfigFile("ide.abbrs")); + try { + CParser p(s); + while(!p.IsEof()) { + String a = p.ReadString(); + p.Char('='); + String b = p.ReadString(); + p.Char(';'); + abbr.Add(a, b); + } + } + catch(CParser::Error) {} + if(abbr.GetCount() == 0) { + abbr.Add("c", "case @:\r\n\tbreak;"); + abbr.Add("d", "do\r\n\t@;\r\nwhile();"); + abbr.Add("db", "do {\r\n\t@;\r\n}\r\nwhile();"); + abbr.Add("e", "else\r\n\t;"); + abbr.Add("eb", "else {\r\n\t;\r\n}"); + abbr.Add("ei", "else\r\nif(@)\r\n\t;"); + abbr.Add("eib", "else\r\nif(@) {\r\n\t;\r\n}"); + abbr.Add("f", "for(@;;)\r\n\t;"); + abbr.Add("fb", "for(@;;) {\r\n\t\r\n}"); + abbr.Add("i", "if(@)\r\n\t;"); + abbr.Add("ib", "if(@) {\r\n\t\r\n}"); + abbr.Add("ie", "if(@)\r\n\t;\r\nelse\r\n\t;"); + abbr.Add("ieb", "if(@) {\r\n\t\r\n}\r\nelse {\r\n\t\r\n}"); + abbr.Add("sw", "switch(@) {\r\ncase :\r\n\tbreak;\r\ndefault:\r\n\tbreak;\r\n}"); + abbr.Add("w", "while(@)\r\n\t;"); + abbr.Add("wb", "while(@) {\r\n\t\r\n}"); + } +} + +void Ide::SaveAbbr() +{ + String r; + for(int i = 0; i < abbr.GetCount(); i++) + r << AsCString(abbr.GetKey(i)) << '=' << AsCString(abbr[i]) << ";\r\n"; + UPP::SaveFile(ConfigFile("ide.abbrs"), r); +} diff --git a/uppsrc/ide/Assist.cpp b/uppsrc/ide/Assist.cpp index c950415f4..8b77142e0 100644 --- a/uppsrc/ide/Assist.cpp +++ b/uppsrc/ide/Assist.cpp @@ -457,6 +457,47 @@ void AssistEditor::Complete() PopUpAssist(true); } +void AssistEditor::Abbr() +{ + CloseAssist(); + int c = GetCursor(); + int ch; + String s; + while(IsAlpha(ch = Ch(c - 1))) { + s.Insert(0, ch); + --c; + } + int len = s.GetCount(); + s = theide->abbr.Get(s, String()); + if(IsNull(s)) + return; + NextUndo(); + SetCursor(c); + Remove(c, len); + int linepos = c; + int line = GetLinePos(linepos); + WString h = GetWLine(line).Mid(0, linepos); + DDUMP(h); + for(int i = 0; i < s.GetCount(); i++) { + ch = s[i]; + switch(ch) { + case '@': + c = GetCursor(); + break; + case '\n': + InsertChar('\n'); + for(int j = 0; j < h.GetCount(); j++) + InsertChar(h[j]); + break; + default: + if((byte)s[i] >= ' ' || s[i] == '\t') + InsertChar(s[i]); + break; + } + } + SetCursor(c); +} + void AssistEditor::AssistInsert() { if(assist.IsCursor()) { diff --git a/uppsrc/ide/ide.h b/uppsrc/ide/ide.h index f65e42e53..b4bff9172 100644 --- a/uppsrc/ide/ide.h +++ b/uppsrc/ide/ide.h @@ -433,6 +433,7 @@ struct AssistEditor : CodeEditor { void StartParamInfo(const CppItem& m); void Complete(); + void Abbr(); void Context(Parser& parser, int pos); void ExpressionType(const String& type, const Vector& xp, int ii, @@ -783,6 +784,7 @@ public: int state_icon; String export_dir; + VectorMap abbr; // ------------------------------------ @@ -848,6 +850,9 @@ public: void GotoPos(String path, int line); void GotoCpp(const CppItem& pos); + + void LoadAbbr(); + void SaveAbbr(); Vector SvnDirs(); @@ -980,6 +985,7 @@ public: void SetupFormat(); void ToggleVerboseBuild(); void AutoSetup(); + void Abbreviations(); void BrowseMenu(Bar& menu); void RescanCode(); diff --git a/uppsrc/ide/ide.key b/uppsrc/ide/ide.key index 48dcfd86f..9d643c0b9 100644 --- a/uppsrc/ide/ide.key +++ b/uppsrc/ide/ide.key @@ -79,7 +79,7 @@ KEY(SEARCHTOPICS, "Search word in Help & Topics", K_CTRL_F1) KEY(BROWSETOPICS, "Help & Topics", K_F1) KEY(CALC, "Calculator", K_CTRL_E) KEY(ASSIST, "Assist", ' '|K_CTRL) -KEY(COMPLETE, "Complete identifier", K_CTRL_COMMA) +KEY(ABBR, "Complete abbreviation", K_CTRL_PERIOD) KEY(DCOPY, "Copy as definition/declaration", K_ALT_C) KEY(VIRTUALS, "Virtual methods..", K_ALT_V) KEY(THISBACKS, "THISBACKs..", K_ALT_T) @@ -90,6 +90,7 @@ KEY(CLEARMARKERSALL, "Remove all change markers", K_SHIFT|K_ALT_R) KEY(TOGGLEINDEX, "File index", K_CTRL_F12) KEY(SEARCHINDEX, "File index search", K_F12) KEY(SEARCHCODE, "Search code", K_CTRL_Q) +KEY(COMPLETE, "Complete identifier", K_CTRL_COMMA) KEY(TOUPPER, "To uppercase", 0) KEY(TOLOWER, "To lowercase", 0) diff --git a/uppsrc/ide/ide.lay b/uppsrc/ide/ide.lay index 30d17d702..8b294a0e4 100644 --- a/uppsrc/ide/ide.lay +++ b/uppsrc/ide/ide.lay @@ -566,3 +566,14 @@ LAYOUT(InsertColorLayout, 432, 520) ITEM(Button, cancel, SetLabel(t_("Cancel")).LeftPosZ(284, 144).TopPosZ(492, 24)) END_LAYOUT +LAYOUT(AbbreviationsLayout, 424, 476) + ITEM(ArrayCtrl, abbr, LeftPosZ(4, 108).TopPosZ(4, 432)) + ITEM(CodeEditor, code, LeftPosZ(116, 304).TopPosZ(4, 408)) + ITEM(Button, add, SetLabel(t_("Add")).LeftPosZ(4, 64).TopPosZ(444, 24)) + ITEM(Button, edit, SetLabel(t_("Edit")).LeftPosZ(72, 64).TopPosZ(444, 24)) + ITEM(Button, remove, SetLabel(t_("Remove")).LeftPosZ(140, 64).TopPosZ(444, 24)) + ITEM(Button, ok, SetLabel(t_("OK")).LeftPosZ(288, 64).TopPosZ(444, 24)) + ITEM(Button, cancel, SetLabel(t_("Cancel")).LeftPosZ(356, 64).TopPosZ(444, 24)) + ITEM(Label, dv___7, SetLabel(t_("Use '@' character to place the caret.")).LeftPosZ(116, 304).TopPosZ(416, 19)) +END_LAYOUT + diff --git a/uppsrc/ide/ide.upp b/uppsrc/ide/ide.upp index eb9457435..77aeb9dae 100644 --- a/uppsrc/ide/ide.upp +++ b/uppsrc/ide/ide.upp @@ -58,6 +58,7 @@ file Macro.cpp, Calc.cpp, FormatCode.cpp, + Abbr.cpp, Compile readonly separator, Methods.cpp, AutoSetup.cpp, diff --git a/uppsrc/ide/idebar.cpp b/uppsrc/ide/idebar.cpp index cf33b2b9e..481ae3543 100644 --- a/uppsrc/ide/idebar.cpp +++ b/uppsrc/ide/idebar.cpp @@ -276,6 +276,8 @@ void Ide::Setup(Bar& menu) { .Help("Log detailed description of build and debug"); menu.Add("Environment..", THISBACK(SetupFormat)) .Help("Fonts, tabs, indentation, status bar"); + menu.Add("Abbreviations..", THISBACK(Abbreviations)) + .Help("Edit abbreviation keywords and code"); menu.Add("Keyboard shortcuts..", callback(EditKeys)) .Help("Edit key bindings"); menu.Add("Build methods..", THISBACK(SetupBuildMethods)) @@ -474,6 +476,7 @@ void Ide::BrowseMenu(Bar& menu) { menu.Add(!designer, AK_VIRTUALS, callback(&editor, &AssistEditor::Virtuals)); menu.Add(!designer, AK_THISBACKS, callback(&editor, &AssistEditor::Thisbacks)); menu.Add(!designer, AK_COMPLETE, callback(&editor, &AssistEditor::Complete)); + menu.Add(!designer, AK_ABBR, callback(&editor, &AssistEditor::Abbr)); menu.Add(!designer, "Insert", THISBACK(InsertMenu)); menu.Separator(); menu.Add("Rescan code", THISBACK(RescanCode)); diff --git a/uppsrc/ide/idewin.cpp b/uppsrc/ide/idewin.cpp index 5c5987276..a79deabae 100644 --- a/uppsrc/ide/idewin.cpp +++ b/uppsrc/ide/idewin.cpp @@ -933,6 +933,7 @@ void AppMain___() } LoadFromFile(ide); + ide.LoadAbbr(); ide.SyncCh();