Skip to content

Commit

Permalink
Improve code completion in editor
Browse files Browse the repository at this point in the history
  • Loading branch information
Blake-Madden committed Sep 30, 2024
1 parent 6361385 commit 4492045
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 19 deletions.
75 changes: 59 additions & 16 deletions src/ui/controls/codeeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ long CodeEditor::FindNext(const wxString& textToFind, const int searchFlags /*=
//-------------------------------------------------------------
void CodeEditor::AddFunctionsOrClasses(const NameList& functions)
{
ResetActiveFunctionMap();

for (const auto& func : functions)
{
m_libaryAndClassNames.insert(StripExtraInfo(func.c_str()));
Expand All @@ -354,31 +356,42 @@ void CodeEditor::AddFunctionsOrClasses(const NameList& functions)
//-------------------------------------------------------------
void CodeEditor::AddLibrary(const wxString& library, NameList& functions)
{
ResetActiveFunctionMap();

wxString functionString;
wxString returnTypeStr;
wxStringNoCaseMap funcNamesAndSignatures;
for (const auto& func : functions)
{
functionString += L" " + StripExtraInfo(func);
returnTypeStr = GetReturnType(func);
const wxString funcName = StripExtraInfo(func);
const wxString returnTypeStr = GetReturnType(func);
if (returnTypeStr.length())
{
m_libraryFunctionsWithReturnTypes.insert(
std::make_pair(library + L"." + StripExtraInfo(func), returnTypeStr));
}
funcNamesAndSignatures.emplace(funcName, StripReturnType(func));
functionString += L" " + funcName;
}
m_libraryCollection.insert(std::make_pair(library, functionString));
m_libraryCollection.insert(
std::make_pair(library, std::make_pair(functionString, funcNamesAndSignatures)));
m_libaryAndClassNames.insert(library);
}

//-------------------------------------------------------------
void CodeEditor::AddClass(const wxString& theClass, NameList& functions)
{
ResetActiveFunctionMap();

wxString functionString;
wxStringNoCaseMap funcNamesAndSignatures;
for (const auto& func : functions)
{
functionString += L" " + StripExtraInfo(func);
const wxString funcName = StripExtraInfo(func);
funcNamesAndSignatures.emplace(funcName, StripReturnType(func));
functionString += L" " + funcName;
}
m_classCollection.insert(std::make_pair(theClass, functionString));
m_classCollection.insert(
std::make_pair(theClass, std::make_pair(functionString, funcNamesAndSignatures)));
m_libaryAndClassNames.insert(theClass);
}

Expand Down Expand Up @@ -414,6 +427,20 @@ wxString CodeEditor::StripExtraInfo(const wxString& function)
}
}

//-------------------------------------------------------------
wxString CodeEditor::StripReturnType(const wxString& function)
{
const auto retSepStart = function.find(L"->");
if (retSepStart != wxString::npos)
{
return function.substr(0, retSepStart);
}
else
{
return function;
}
}

//-------------------------------------------------------------
wxString CodeEditor::GetReturnType(const wxString& function)
{
Expand Down Expand Up @@ -474,7 +501,8 @@ void CodeEditor::OnCharAdded(wxStyledTextEvent& event)
auto pos = m_libraryCollection.find(lastWord);
if (pos != m_libraryCollection.cend())
{
AutoCompShow(0, pos->second);
m_activeFunctionsAndSignaturesMap = &pos->second.second;
AutoCompShow(0, pos->second.first);
}
}
else if (event.GetKey() == L')' || event.GetKey() == L'(')
Expand All @@ -495,10 +523,11 @@ void CodeEditor::OnCharAdded(wxStyledTextEvent& event)
auto libraryPos = m_libraryFunctionsWithReturnTypes.find(libraryName);
if (libraryPos != m_libraryFunctionsWithReturnTypes.cend())
{
libraryPos = m_classCollection.find(libraryPos->second);
if (libraryPos != m_classCollection.cend())
auto classPos = m_classCollection.find(libraryPos->second);
if (classPos != m_classCollection.cend())
{
AutoCompShow(0, libraryPos->second);
m_activeFunctionsAndSignaturesMap = &classPos->second.second;
AutoCompShow(0, classPos->second.first);
}
}
}
Expand Down Expand Up @@ -530,7 +559,8 @@ void CodeEditor::OnCharAdded(wxStyledTextEvent& event)
const auto classPos = m_classCollection.find(assignment);
if (classPos != m_classCollection.cend())
{
AutoCompShow(0, classPos->second);
m_activeFunctionsAndSignaturesMap = &classPos->second.second;
AutoCompShow(0, classPos->second.first);
break;
}
else
Expand All @@ -556,21 +586,22 @@ void CodeEditor::OnCharAdded(wxStyledTextEvent& event)

if (lastWord.length())
{
// see if we are inside a library, if so show its list of functions
// see if we are inside a library; if so show its list of functions
if (wordStart > 2 && GetCharAt(wordStart - 1) == GetLibraryAccessor())
{
const wxString libraryName =
GetTextRange(WordStartPosition(wordStart - 2, true), wordStart - 1);
auto pos = m_libraryCollection.find(libraryName);
if (pos != m_libraryCollection.cend())
{
m_activeFunctionsAndSignaturesMap = &pos->second.second;
if (AutoCompActive())
{
AutoCompSelect(lastWord);
}
else
{
AutoCompShow(lastWord.length(), pos->second);
AutoCompShow(lastWord.length(), pos->second.first);
}
}
}
Expand All @@ -590,16 +621,17 @@ void CodeEditor::OnCharAdded(wxStyledTextEvent& event)
auto libraryPos = m_libraryFunctionsWithReturnTypes.find(libraryName);
if (libraryPos != m_libraryFunctionsWithReturnTypes.cend())
{
libraryPos = m_classCollection.find(libraryPos->second);
if (libraryPos != m_classCollection.cend())
auto classPos = m_classCollection.find(libraryPos->second);
if (classPos != m_classCollection.cend())
{
m_activeFunctionsAndSignaturesMap = &classPos->second.second;
if (AutoCompActive())
{
AutoCompSelect(lastWord);
}
else
{
AutoCompShow(lastWord.length(), libraryPos->second);
AutoCompShow(lastWord.length(), classPos->second.first);
}
}
}
Expand Down Expand Up @@ -669,6 +701,16 @@ void CodeEditor::OnCharAdded(wxStyledTextEvent& event)
void CodeEditor::OnAutoCompletionSelected(wxStyledTextEvent& event)
{
wxString selectedString = event.GetText();

if (m_activeFunctionsAndSignaturesMap != nullptr)
{
const auto foundFunc = m_activeFunctionsAndSignaturesMap->find(selectedString);
if (foundFunc != m_activeFunctionsAndSignaturesMap->cend())
{
// use the full function signature instead of just its name
selectedString = foundFunc->second;
}
}
wxString paramText;
const bool hasParams = SplitFunctionAndParams(selectedString, paramText);

Expand All @@ -687,4 +729,5 @@ void CodeEditor::OnAutoCompletionSelected(wxStyledTextEvent& event)
paramText += L")";
CallTipShow(GetCurrentPos(), paramText);
}
ResetActiveFunctionMap();
}
18 changes: 15 additions & 3 deletions src/ui/controls/codeeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,19 +255,31 @@ namespace Wisteria::UI
[[nodiscard]]
static wxString StripExtraInfo(const wxString& function);
[[nodiscard]]
static wxString StripReturnType(const wxString& function);
[[nodiscard]]
static wxString GetReturnType(const wxString& function);

void OnMarginClick(wxStyledTextEvent& event);
void OnCharAdded(wxStyledTextEvent& event);
void OnAutoCompletionSelected(wxStyledTextEvent& event);
void OnKeyDown(wxKeyEvent& event);

std::map<wxString, wxString, wxStringCmpNoCase> m_libraryCollection;
std::map<wxString, wxString, wxStringCmpNoCase> m_classCollection;
std::map<wxString, wxString, wxStringCmpNoCase> m_libraryFunctionsWithReturnTypes;
void ResetActiveFunctionMap() noexcept { m_activeFunctionsAndSignaturesMap = nullptr; }

using wxStringNoCaseMap = std::map<wxString, wxString, wxStringCmpNoCase>;
// Library name, string of function names, and map of function names
// and their respective full signature
using ObjectAndFunctionsMap = std::map<wxString, std::pair<wxString, wxStringNoCaseMap>, wxStringCmpNoCase>;

ObjectAndFunctionsMap m_libraryCollection;
ObjectAndFunctionsMap m_classCollection;

wxStringNoCaseMap m_libraryFunctionsWithReturnTypes;
std::set<wxString, wxStringPartialCmpNoCase> m_libaryAndClassNames;
wxString m_libaryAndClassNamesStr;

const wxStringNoCaseMap* m_activeFunctionsAndSignaturesMap{ nullptr };

int m_lexer{ wxSTC_LEX_LUA };

wxString m_scriptFilePath;
Expand Down

0 comments on commit 4492045

Please sign in to comment.