Skip to content

Commit d90772a

Browse files
committed
Fix Docstring Context Extraction And Add JSON Contains Alias
Improve docstring generation by gathering a safer text range from the current symbol. When the symbol spans a single line, the full line is now used directly; for multi-line symbols, the range includes the complete ending line. An explicit warning is shown and generation is aborted if no usable context can be collected. Also add a clearer JSON object membership API via `JSONItem::contains`, with `hasNamedObject` preserved as a thin alias for compatibility. While touching the JSON helpers, the membership lookup now uses a UTF-8 string conversion path that matches the underlying cJSON call more directly. * AI docstring generation * JSON utilities **Generated by CodeLite** Signed-off-by: Eran Ifrah <eran@codelite.org>
1 parent 5bd08b2 commit d90772a

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

CodeLite/JSON.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@
2929

3030
#include <wx/filename.h>
3131

32-
JSON::JSON(const wxString& text)
33-
{
34-
m_json = cJSON_Parse(text.mb_str(wxConvUTF8).data());
35-
}
32+
JSON::JSON(const wxString& text) { m_json = cJSON_Parse(text.mb_str(wxConvUTF8).data()); }
3633

3734
JSON::JSON(JsonType type)
3835
{
@@ -413,15 +410,16 @@ wxArrayString JSONItem::toArrayString(const wxArrayString& defaultValue) const
413410
return arr;
414411
}
415412

416-
bool JSONItem::hasNamedObject(const wxString& name) const
413+
bool JSONItem::contains(const wxString& name) const
417414
{
418415
if (!m_json) {
419416
return false;
420417
}
421418

422-
cJSON* obj = cJSON_GetObjectItem(m_json, name.mb_str(wxConvUTF8).data());
423-
return obj != nullptr;
419+
std::string name_cstr = name.ToStdString(wxConvUTF8);
420+
return cJSON_GetObjectItem(m_json, name_cstr.c_str()) != nullptr;
424421
}
422+
425423
#if wxUSE_GUI
426424

427425
JSONItem& JSONItem::addProperty(const wxString& name, const wxSize& sz)

CodeLite/JSON.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,18 @@ class WXDLLIMPEXP_CL JSONItem
6060
// Readers
6161
////////////////////////////////////////////////
6262
JSONItem namedObject(const wxString& name) const;
63-
bool hasNamedObject(const wxString& name) const;
63+
/**
64+
* @brief Checks whether this JSON object contains a member with the specified name.
65+
*
66+
* @param name wxString The name of the member to search for in the JSON object.
67+
* @return bool true if the object contains a member with the given name; otherwise false.
68+
*/
69+
bool contains(const wxString& name) const;
70+
71+
/**
72+
* @brief An alias to the `contains` method above.
73+
*/
74+
bool hasNamedObject(const wxString& name) const { return contains(name); }
6475

6576
/// If your array is big (hundred of entries) use
6677
/// `GetAsVector` and iterate it instead

Plugin/ai/LLMManager.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,9 +1343,24 @@ void Manager::OnGenerateDocString(wxCommandEvent& event)
13431343
return;
13441344
}
13451345

1346-
int start_pos = stc->PositionFromLine(symbol_range.value().start_line - 1);
1347-
int end_pos = stc->PositionFromLine(symbol_range.value().end_line.value_or(stc->GetLineCount()) - 1);
1348-
wxString func_text = stc->GetTextRange(start_pos, end_pos);
1346+
int start_line = symbol_range.value().start_line - 1;
1347+
int end_line = symbol_range.value().end_line.value_or(stc->GetLineCount()) - 1;
1348+
1349+
wxString func_text;
1350+
if (start_line == end_line) {
1351+
// take the entire line
1352+
func_text = stc->GetLine(start_line);
1353+
} else {
1354+
// Take everything between the start and end line.
1355+
int start_pos = stc->PositionFromLine(start_line);
1356+
int end_pos = stc->PositionFromLine(end_line) + stc->LineLength(end_line);
1357+
func_text = stc->GetTextRange(start_pos, end_pos);
1358+
}
1359+
1360+
if (func_text.empty()) {
1361+
::clMessageBox(_("Unable to gather enough context to generate docstring"), "CodeLite", wxOK | wxICON_WARNING);
1362+
return;
1363+
}
13491364

13501365
clGetManager()->SetStatusMessage(_("Generating DocString..."), 1);
13511366
wxString prompt = GetConfig().GetPrompt(llm::PromptKind::kCommentGeneration);

0 commit comments

Comments
 (0)