Skip to content

Commit

Permalink
Fixed: function argument tip window no longer jumps when one of the a…
Browse files Browse the repository at this point in the history
…rguments has "(" in it
  • Loading branch information
eranif committed Apr 7, 2024
1 parent 0655eb2 commit 92f1bf5
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 125 deletions.
4 changes: 4 additions & 0 deletions LiteEditor/cl_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4784,6 +4784,10 @@ void clEditor::DoCancelCalltip()

int clEditor::DoGetOpenBracePos()
{
if (m_calltip && m_calltip->IsShown()) {
return m_calltip->GetEditorStartPosition();
}

// determine the closest open brace from the current caret position
int depth(0);
int char_tested(0); // we add another performance tuning here: dont test more than 256 characters backward
Expand Down
69 changes: 35 additions & 34 deletions Plugin/cc_box_tip_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ double m_ratio = 0.0;

void InflateSize(wxSize& size, double factor)
{
if(factor > 1.0) {
if (factor > 1.0) {
double updated_w = factor * (double)size.GetWidth();
double updated_h = factor * (double)size.GetHeight();
size.SetWidth(updated_w);
Expand All @@ -76,18 +76,18 @@ void CCBoxTipWindow_ShrinkTip(wxString& str, bool strip_html_tags)
str.Replace("/*!", wxEmptyString);
str.Replace("*/", wxEmptyString);

if(strip_html_tags && re.IsValid()) {
if (strip_html_tags && re.IsValid()) {
re.ReplaceAll(&str, wxEmptyString);
}

wxString curline;
wxArrayString lines;
enum State { kNormal, kCodeBlockLanguage, kCodeBlock } state = kNormal;
for(const wxChar& ch : str) {
switch(state) {
for (const wxChar& ch : str) {
switch (state) {
case kNormal:
if(curline.empty()) {
switch(ch) {
if (curline.empty()) {
switch (ch) {
// ignore leading white spaces
case '\n':
case '\r':
Expand All @@ -101,7 +101,7 @@ void CCBoxTipWindow_ShrinkTip(wxString& str, bool strip_html_tags)
continue;
} else {
// curline is not empty
switch(ch) {
switch (ch) {
case '\n':
lines.Add(curline);
curline.clear();
Expand All @@ -112,14 +112,14 @@ void CCBoxTipWindow_ShrinkTip(wxString& str, bool strip_html_tags)
case ',':
// check for word wrapping after the above characters
curline << ch;
if(curline.size() >= MAX_LINE_WIDTH) {
if (curline.size() >= MAX_LINE_WIDTH) {
lines.Add(curline);
curline.clear();
}
break;
default:
curline << ch;
if(curline == "```") {
if (curline == "```") {
// starting a codeblock
state = kCodeBlockLanguage;
lines.Add(curline);
Expand All @@ -132,7 +132,7 @@ void CCBoxTipWindow_ShrinkTip(wxString& str, bool strip_html_tags)
case kCodeBlockLanguage:
// sometimes, a codeblock prefix is followed by the language
// skip it (e.g. "```cpp")
switch(ch) {
switch (ch) {
case '\n':
state = kCodeBlock;
break;
Expand All @@ -142,7 +142,7 @@ void CCBoxTipWindow_ShrinkTip(wxString& str, bool strip_html_tags)
break;
case kCodeBlock:
// Unformatted code block. Only handle LF and the special char backslash
switch(ch) {
switch (ch) {
case '\\':
break;
case '\n':
Expand All @@ -151,7 +151,7 @@ void CCBoxTipWindow_ShrinkTip(wxString& str, bool strip_html_tags)
break;
default:
curline << ch;
if(curline.EndsWith("```")) {
if (curline.EndsWith("```")) {
lines.Add(curline);
curline.clear();
state = kNormal;
Expand All @@ -163,7 +163,7 @@ void CCBoxTipWindow_ShrinkTip(wxString& str, bool strip_html_tags)
}

// if we got some unprocessed line -> add it
if(!curline.empty()) {
if (!curline.empty()) {
lines.Add(curline);
curline.clear();
}
Expand All @@ -181,7 +181,7 @@ std::unique_ptr<wxDisplay> GetDisplay(const wxWindow* win)
#else
wxDisplay* d = nullptr;
int index = wxDisplay::GetFromWindow(win);
if(index == wxNOT_FOUND) {
if (index == wxNOT_FOUND) {
d = new wxDisplay();
} else {
d = new wxDisplay(index);
Expand Down Expand Up @@ -213,7 +213,7 @@ void CCBoxTipWindow::DoInitialize(size_t numOfTips)

clMarkdownRenderer renderer;
wxRect text_rect = renderer.GetSize(this, dc, m_tip);
if(m_ratio > 0.0) {
if (m_ratio > 0.0) {
wxSize sz = text_rect.GetSize();
InflateSize(sz, m_ratio);
text_rect.SetSize(sz);
Expand All @@ -236,34 +236,35 @@ void CCBoxTipWindow::ShrinkToScreen(wxSize& size) const
// shrink up to the client area to preserve an extra space to un-hover it
wxRect display_rect = display->GetClientArea();

if(size.GetHeight() > display_rect.GetHeight()) {
if (size.GetHeight() > display_rect.GetHeight()) {
size.SetHeight(display_rect.GetHeight());
}
if(size.GetWidth() >= display_rect.GetWidth()) {
if (size.GetWidth() >= display_rect.GetWidth()) {
size.SetWidth(display_rect.GetWidth());
}
}

void CCBoxTipWindow::PositionRelativeTo(wxWindow* win, wxPoint caretPos, IEditor* focusEdior)
void CCBoxTipWindow::PositionRelativeTo(wxWindow* win, wxPoint caretPos, int start_position, IEditor* focusEdior)
{
// When shown, set the focus back to the editor
m_editorStartPosition = start_position;
wxPoint pt = win->GetScreenPosition();
wxPoint windowPos = pt;
wxSize ccBoxSize = win->GetSize();
wxSize tipSize = GetSize();
pt.x += ccBoxSize.x;
int lineHeight = 20;
wxStyledTextCtrl* stc = nullptr;
if(focusEdior) {
if (focusEdior) {
stc = focusEdior->GetCtrl();
} else {
IEditor* editor = clGetManager()->GetActiveEditor();
if(editor) {
if (editor) {
stc = editor->GetCtrl();
}
}

if(stc) {
if (stc) {
lineHeight = stc->TextHeight(stc->GetCurrentLine());
}

Expand All @@ -274,56 +275,56 @@ void CCBoxTipWindow::PositionRelativeTo(wxWindow* win, wxPoint caretPos, IEditor
auto display = GetDisplay(this);
wxRect displaySize = display->GetGeometry();

if((pt.x + tipSize.x) > (displaySize.GetX() + displaySize.GetWidth())) {
if ((pt.x + tipSize.x) > (displaySize.GetX() + displaySize.GetWidth())) {
// Move the tip to the left
pt = windowPos;
pt.x -= tipSize.x;

if(pt.x < 0) {
if (pt.x < 0) {
// it cant be placed on the left side either
// try placing it on top of the completion box
pt = windowPos;
vPositioned = true;
pt.y -= tipSize.y;
if(!ccBoxIsAboveCaretLine) {
if (!ccBoxIsAboveCaretLine) {
pt.y -= lineHeight; // The CC box is placed under the caret line, but the tip will be placed
// on top of the CC box
}

if(pt.y < 0) {
if (pt.y < 0) {
// try placing under the completion box
pt = windowPos;
pt.y += ccBoxSize.y + 1;
if(ccBoxIsAboveCaretLine) {
if (ccBoxIsAboveCaretLine) {
pt.y += lineHeight; // dont hide the caret line
}
}
}
}

if(!vPositioned) {
if (!vPositioned) {
// The tip window is positioned to the left or right of the CC box
// Check if the tip window is going outside of the display, if it is, move it up
if((pt.y + tipSize.GetHeight()) > displaySize.GetHeight()) {
if ((pt.y + tipSize.GetHeight()) > displaySize.GetHeight()) {
pt.y = (displaySize.GetHeight() - tipSize.GetHeight());
// Make sure that the top of the tip is always visible
pt.y = std::max(0, pt.y);
}
}

if(focusEdior) {
if (focusEdior) {
// Check that the tip Y coord is inside the editor
// this is to prevent some zombie tips appearing floating in no-man-land
wxRect editorRect = focusEdior->GetCtrl()->GetScreenRect();
if(editorRect.GetTopLeft().y > pt.y) {
if (editorRect.GetTopLeft().y > pt.y) {
return;
}
}

SetSize(wxRect(pt, GetSize()));
Show();

if(focusEdior) {
if (focusEdior) {
focusEdior->SetActive();
}
}
Expand All @@ -342,7 +343,7 @@ void CCBoxTipWindow::PositionAt(const wxPoint& pt, IEditor* focusEdior)
SetSize(wxRect(pt, GetSize()));
Show();

if(focusEdior) {
if (focusEdior) {
focusEdior->SetActive();
}
}
Expand All @@ -356,7 +357,7 @@ void CCBoxTipWindow::PositionLeftTo(wxWindow* win, IEditor* focusEditor)
SetSize(wxRect(pt, GetSize()));
Show();

if(focusEditor) {
if (focusEditor) {
focusEditor->SetActive();
}
}
Expand All @@ -369,7 +370,7 @@ void CCBoxTipWindow::DoDrawTip(wxDC& dc)

ShrinkToScreen(size);

if(m_ratio == 0.0 && size.GetWidth() > client_rect.GetWidth()) {
if (m_ratio == 0.0 && size.GetWidth() > client_rect.GetWidth()) {
m_ratio = (double)size.GetWidth() / (double)client_rect.GetWidth();
m_ratio += 0.01; // give it some extra space

Expand Down
5 changes: 4 additions & 1 deletion Plugin/cc_box_tip_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class WXDLLIMPEXP_SDK CCBoxTipWindow : public wxPopupWindow
wxString m_tip;
size_t m_numOfTips = 1;
bool m_stripHtmlTags = false;
int m_editorStartPosition = wxNOT_FOUND;

protected:
void OnPaint(wxPaintEvent& e);
Expand All @@ -59,7 +60,7 @@ class WXDLLIMPEXP_SDK CCBoxTipWindow : public wxPopupWindow
* if focusEditor is NOT null, the editor will gain the focus once
* the tip is shown
*/
void PositionRelativeTo(wxWindow* win, wxPoint caretPos, IEditor* focusEdior = NULL);
void PositionRelativeTo(wxWindow* win, wxPoint caretPos, int start_position, IEditor* focusEdior = NULL);

/**
* @brief position this window to the left of 'win'
Expand All @@ -71,6 +72,8 @@ class WXDLLIMPEXP_SDK CCBoxTipWindow : public wxPopupWindow
* @brief position and show the tip at a given location
*/
void PositionAt(const wxPoint& pt, IEditor* focusEdior = NULL);

int GetEditorStartPosition() const { return m_editorStartPosition; }
};

#endif // CCBOXTIPWINDOW_H

0 comments on commit 92f1bf5

Please sign in to comment.