Skip to content

Commit

Permalink
implemented IDLE status for Editor: the status triggers every 500ms a… (
Browse files Browse the repository at this point in the history
#348)

* implemented IDLE status for Editor: the status triggers every 500ms after the last document change

* Also fix memory leak
---------

Co-authored-by: Stefano Ceccherini <3501519+jackburton79@users.noreply.github.com>
Co-authored-by: Jackburton79 <stefano.ceccherini@gmail.com>
  • Loading branch information
3 people authored Apr 19, 2024
1 parent 9ee3078 commit 74e0c32
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 63 deletions.
53 changes: 39 additions & 14 deletions src/lsp-client/LSPEditorWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

#include <Application.h>
#include <Path.h>
#include <Window.h>
#include <Catalog.h>
#include <Window.h>

#include <algorithm>
#include <cstdio>
Expand Down Expand Up @@ -82,12 +82,14 @@ LSPEditorWrapper::HasLSPServer()
return (fLSPProjectWrapper != nullptr);
}


bool
LSPEditorWrapper::HasLSPServerCapability(const LSPCapability cap)
{
return (HasLSPServer() && fLSPProjectWrapper->HasCapability(cap));
}


void
LSPEditorWrapper::ApplyFix(BMessage* info)
{
Expand Down Expand Up @@ -145,12 +147,14 @@ LSPEditorWrapper::SetLSPServer(LSPProjectWrapper* cW) {
}
}


bool
LSPEditorWrapper::IsInitialized()
{
return (fInitialized && fLSPProjectWrapper != nullptr);
}


void
LSPEditorWrapper::didOpen()
{
Expand All @@ -170,7 +174,6 @@ LSPEditorWrapper::didClose()
if (!IsInitialized())
return;


if (fEditor) {
_RemoveAllDiagnostics();
_RemoveAllDocumentLinks();
Expand All @@ -186,7 +189,7 @@ LSPEditorWrapper::didSave()
if (!IsInitialized())
return;


flushChanges();
fLSPProjectWrapper->DidSave(this);
}

Expand All @@ -208,9 +211,17 @@ LSPEditorWrapper::didChange(
event.range = range;
event.text.assign(text, len);

std::vector<TextDocumentContentChangeEvent> changes{event};
fChanges.push_back(event);
}

fLSPProjectWrapper->DidChange(this, changes, false);

void
LSPEditorWrapper::flushChanges()
{
if (fChanges.size() > 0) {
fLSPProjectWrapper->DidChange(this, fChanges, false);
fChanges.clear();
}
}


Expand Down Expand Up @@ -314,6 +325,7 @@ LSPEditorWrapper::StartHover(Sci_Position sci_position)
fLSPProjectWrapper->Hover(this, position);
}


int32
LSPEditorWrapper::DiagnosticFromPosition(Sci_Position sci_position, LSPDiagnostic& dia)
{
Expand All @@ -330,6 +342,7 @@ LSPEditorWrapper::DiagnosticFromPosition(Sci_Position sci_position, LSPDiagnosti
return -1;
}


int32
LSPEditorWrapper::DiagnosticFromRange(Range& range, LSPDiagnostic& dia)
{
Expand Down Expand Up @@ -454,18 +467,21 @@ LSPEditorWrapper::StartCompletion()
fLSPProjectWrapper->Completion(this, position, context);
}


void
LSPEditorWrapper::NextCallTip()
{
fCallTip.NextCallTip();
}


void
LSPEditorWrapper::PrevCallTip()
{
fCallTip.PrevCallTip();
}


void
LSPEditorWrapper::RequestDocumentSymbols()
{
Expand Down Expand Up @@ -495,6 +511,7 @@ LSPEditorWrapper::CharAdded(const char ch /*utf-8?*/)
if (fCallTip.IsVisible())
fCallTip.HideCallTip();

flushChanges();
StartCompletion();
}
}
Expand All @@ -503,6 +520,7 @@ LSPEditorWrapper::CharAdded(const char ch /*utf-8?*/)
CallTipAction action = fCallTip.UpdateCallTip(ch, ch == 0);
if (action == CALLTIP_NEWDATA) {

flushChanges();
Position lsp_position;
FromSciPositionToLSPPosition(fCallTip.Position(), &lsp_position);
fLSPProjectWrapper->SignatureHelp(this, lsp_position);
Expand Down Expand Up @@ -604,7 +622,6 @@ LSPEditorWrapper::_DoSwitchSourceHeader(json& result)
OpenFileURI(url);
}

#include <stdio.h>

void
LSPEditorWrapper::_DoCompletion(json& params)
Expand Down Expand Up @@ -632,9 +649,7 @@ LSPEditorWrapper::_DoCompletion(json& params)
FromSciPositionToLSPPosition(fCompletionPosition, &pos);
item.textEdit.range.end = pos;

//printf("Debug completion 0 [%s]\n", item.textEdit.newText.c_str());

//funcy algo to find insertText before current position.
// fancy algo to find insertText before current position.
if (position.character == -1) {
line = GetCurrentLine();
GetCurrentLSPPosition(&position);
Expand Down Expand Up @@ -682,6 +697,7 @@ LSPEditorWrapper::_RemoveAllDiagnostics()
fLastDiagnostics.clear();
}


void
LSPEditorWrapper::_DoDiagnostics(nlohmann::json& params)
{
Expand Down Expand Up @@ -721,10 +737,13 @@ LSPEditorWrapper::_DoDiagnostics(nlohmann::json& params)
fEditor->UnlockLooper();
}

if (fLSPProjectWrapper)
if (fLSPProjectWrapper) {
fLSPProjectWrapper->DocumentLink(this);
fLSPProjectWrapper->DocumentSymbol(this);
}
}


void
LSPEditorWrapper::RequestCodeActions(Diagnostic& diagnostic)
{
Expand All @@ -733,17 +752,18 @@ LSPEditorWrapper::RequestCodeActions(Diagnostic& diagnostic)
fLSPProjectWrapper->CodeAction(this, diagnostic.range, context);
}


void
LSPEditorWrapper::CodeActionResolve(value &params)
{
fLSPProjectWrapper->CodeActionResolve(this, params);
}


void
LSPEditorWrapper::_DoCodeActions(nlohmann::json& params)
{
for (auto& v : params) {

CodeAction action;

action.kind = v["kind"].get<std::string>();
Expand All @@ -769,6 +789,7 @@ LSPEditorWrapper::_DoCodeActions(nlohmann::json& params)
}
}


void
LSPEditorWrapper::_DoCodeActionResolve(nlohmann::json& params)
{
Expand Down Expand Up @@ -799,6 +820,7 @@ LSPEditorWrapper::_DoCodeActionResolve(nlohmann::json& params)
}
}


void
LSPEditorWrapper::_RemoveAllDocumentLinks()
{
Expand All @@ -808,13 +830,15 @@ LSPEditorWrapper::_RemoveAllDocumentLinks()
fLastDocumentLinks.clear();
}


void
LSPEditorWrapper::_DoInitialize(nlohmann::json& params)
{
fInitialized = true;
didOpen();
}


void
LSPEditorWrapper::_DoDocumentLink(nlohmann::json& result)
{
Expand Down Expand Up @@ -848,8 +872,7 @@ LSPEditorWrapper::_DoFileStatus(nlohmann::json& params)
void
LSPEditorWrapper::_DoDocumentSymbol(nlohmann::json& params)
{
BMessage msg('symb');

BMessage msg(EDITOR_UPDATE_SYMBOLS);
if (params.is_array() && params.size() > 0) {
if (params[0]["location"].is_null()) {
auto vect = params.get<std::vector<DocumentSymbol>>();
Expand All @@ -861,9 +884,9 @@ LSPEditorWrapper::_DoDocumentSymbol(nlohmann::json& params)
}
if (fEditor != nullptr)
fEditor->SetDocumentSymbols(&msg);

}


void
LSPEditorWrapper::_DoRecursiveDocumentSymbol(std::vector<DocumentSymbol>& vect, BMessage& msg)
{
Expand Down Expand Up @@ -1001,6 +1024,7 @@ LSPEditorWrapper::ApplyTextEdit(json& textEditJson)
return ApplyTextEdit(textEdit);
}


Sci_Position
LSPEditorWrapper::ApplyTextEdit(TextEdit &textEdit)
{
Expand All @@ -1016,6 +1040,7 @@ LSPEditorWrapper::ApplyTextEdit(TextEdit &textEdit)
return s_pos + replaced;
}


void
LSPEditorWrapper::OpenFileURI(std::string uri, int32 line, int32 character, BString edits)
{
Expand Down
3 changes: 2 additions & 1 deletion src/lsp-client/LSPEditorWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class LSPEditorWrapper : public LSPTextDocument {
public:
void didClose();
void didChange(const char* text, long len, Sci_Position start_pos, Sci_Position poslength);
void flushChanges();
void didSave();

void StartCompletion();
Expand Down Expand Up @@ -137,7 +138,7 @@ class LSPEditorWrapper : public LSPTextDocument {
BString edits = "");
std::string GetCurrentLine();
bool IsStatusValid();

std::vector<TextDocumentContentChangeEvent> fChanges;

};

Expand Down
10 changes: 1 addition & 9 deletions src/lsp-client/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -490,15 +490,7 @@ struct DidSaveTextDocumentParams {
};
JSON_SERIALIZE(DidSaveTextDocumentParams, MAP_JSON(MAP_KEY(textDocument)), {});

struct TextDocumentContentChangeEvent {
/// The range of the document that changed.
option<Range> range;

/// The length of the range that got replaced.
//xed option<int> rangeLength;
/// The new text of the range/document.
std::string text;
};
//struct TextDocumentContentChangeEvent
JSON_SERIALIZE(TextDocumentContentChangeEvent, MAP_JSON(MAP_KEY(range)/*, MAP_KEY(rangeLength)*/, MAP_KEY(text)), {});

struct DidChangeTextDocumentParams {
Expand Down
10 changes: 10 additions & 0 deletions src/lsp-client/protocol_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ struct Range {
}
};

struct TextDocumentContentChangeEvent {
/// The range of the document that changed.
option<Range> range;

/// The length of the range that got replaced.
//xed option<int> rangeLength;
/// The new text of the range/document.
std::string text;
};

struct TextEdit {
/// The range of the text document to be manipulated. To insert
/// text into a document create a range where start === end.
Expand Down
31 changes: 31 additions & 0 deletions src/ui/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
namespace Sci = Scintilla;
using namespace Sci::Properties;

const int kIdleTimeout = 500000; //1/2sec

// Differentiate unset parameters from 0 ones
// in scintilla messages
Expand All @@ -68,6 +69,7 @@ Editor::Editor(entry_ref* ref, const BMessenger& target)
, fCurrentColumn(-1)
, fProjectFolder(NULL)
, fSymbolsStatus(STATUS_NOT_INITIALIZED)
, fIdleHandler(nullptr)
{
fStatusView = new editor::StatusView(this);
fFileName = BString(ref->name);
Expand Down Expand Up @@ -156,6 +158,9 @@ void
Editor::MessageReceived(BMessage* message)
{
switch (message->what) {
case kIdle:
fLSPEditorWrapper->flushChanges();
break;
case MSG_REPLACE_ALL:
case MSG_REPLACE_NEXT:
case MSG_REPLACE_ONE:
Expand Down Expand Up @@ -882,6 +887,7 @@ Editor::NotificationReceived(SCNotification* notification)
if (notification->characterSource == SC_CHARACTERSOURCE_DIRECT_INPUT)
fLSPEditorWrapper->CharAdded(notification->ch);
break;

}
case SCN_MARGINCLICK: {
if (notification->margin == sci_BOOKMARK_MARGIN)
Expand All @@ -903,12 +909,14 @@ Editor::NotificationReceived(SCNotification* notification)
case SCN_MODIFIED: {
if (notification->modificationType & SC_MOD_INSERTTEXT) {
fLSPEditorWrapper->didChange(notification->text, notification->length, notification->position, 0);
EvaluateIdleTime();
}
if (notification->modificationType & SC_MOD_BEFOREDELETE) {
fLSPEditorWrapper->didChange("", 0, notification->position, notification->length);
}
if (notification->modificationType & SC_MOD_DELETETEXT) {
fLSPEditorWrapper->CharAdded(0);
EvaluateIdleTime();
}
if (notification->linesAdded != 0)
if (gCFG["show_linenumber"])
Expand Down Expand Up @@ -1943,3 +1951,26 @@ Editor::GetDocumentSymbols(BMessage* symbols) const
symbols->AddInt32("collapsed_kind", iterator->second);
}
}

void
Editor::EvaluateIdleTime()
{
if (fIdleHandler == nullptr || fIdleHandler->SetInterval(kIdleTimeout) != B_OK) {
LogInfo("EvaluateIdleTime: Re-arming IdleHandler...");
if (fIdleHandler != nullptr)
delete fIdleHandler;

// create a message to update the project
BMessage message(kIdle);
fIdleHandler = new BMessageRunner(BMessenger(this), &message, kIdleTimeout, 1);
if (fIdleHandler->InitCheck() != B_OK) {
LogInfo("EvaluateIdleTime: Could not create fIdleHandler. Deleting it");
if (fIdleHandler != nullptr) {
delete fIdleHandler;
fIdleHandler = nullptr;
}
} else {
LogInfo("EvaluateIdleTime: fIdleHandler re-armed.");
}
}
}
Loading

0 comments on commit 74e0c32

Please sign in to comment.