Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 68 additions & 39 deletions liteidex/src/plugins/gopls/goplsplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,7 @@ void GoplsPlugin::appLoaded()
}

resetStandaloneTags();
foreach (LiteApi::IEditor *editor, m_liteApp->editorManager()->editorList()) {
collectStandaloneTags(editor);
}
applyEditorBuildTags(m_liteApp->editorManager()->currentEditor(),false);

QString root = workspaceRoot();
QJsonObject capabilities;
Expand Down Expand Up @@ -275,6 +273,7 @@ void GoplsPlugin::clientInitialized()
if (m_useFeatures) {
setLegacyCompletionEnabled(false);
}
applyEditorBuildTags(m_liteApp->editorManager()->currentEditor(),true);
foreach (LiteApi::IEditor *editor, m_liteApp->editorManager()->editorList()) {
editorCreated(editor);
openDocument(editor);
Expand Down Expand Up @@ -353,12 +352,13 @@ void GoplsPlugin::clientLog(const QString &message, bool error)
bool suppress = !lines.isEmpty();
foreach (QString line, lines) {
line = line.trimmed();
if (line.startsWith(prefix)) {
int index = line.indexOf(prefix);
if (index >= 0) {
if (m_metadataWarnings.contains(line)) {
continue;
}
m_metadataWarnings.insert(line);
handleMissingPackageMetadata(line.mid(prefix.length()).trimmed());
handleMissingPackageMetadata(line.mid(index+prefix.length()).trimmed());
suppress = false;
} else {
suppress = false;
Expand Down Expand Up @@ -397,41 +397,59 @@ QJsonObject GoplsPlugin::goplsSettings() const
foreach (QString tag, m_standaloneTags) {
tags.append(tag);
}
return QJsonObject{{"standaloneTags",tags}};
QJsonArray flags;
if (!m_buildTags.isEmpty()) {
flags.append(QString("-tags=%1").arg(m_buildTags.join(",")));
}
return QJsonObject{{"standaloneTags",tags},{"buildFlags",flags}};
}

void GoplsPlugin::resetStandaloneTags()
{
m_standaloneTags.clear();
m_standaloneTags.append("ignore");
m_buildTags.clear();
}

bool GoplsPlugin::addStandaloneTags(const QStringList &tags)
bool GoplsPlugin::setBuildTags(const QStringList &tags)
{
bool changed = false;
QStringList filtered;
foreach (QString tag, tags) {
if (tag.isEmpty() || m_standaloneTags.contains(tag)) {
if (tag.isEmpty() || filtered.contains(tag)) {
continue;
}
m_standaloneTags.append(tag);
changed = true;
filtered.append(tag);
}
if (changed && m_liteApp) {
m_liteApp->appendLog("Gopls",QString("standaloneTags: %1").arg(m_standaloneTags.join(", ")),false);
if (filtered == m_buildTags) {
return false;
}
return changed;
m_buildTags = filtered;
if (m_liteApp) {
if (m_buildTags.isEmpty()) {
m_liteApp->appendLog("Gopls","buildFlags: (cleared)",false);
} else {
m_liteApp->appendLog("Gopls",QString("buildFlags: -tags=%1").arg(m_buildTags.join(",")),false);
}
}
return true;
}

void GoplsPlugin::collectStandaloneTags(LiteApi::IEditor *editor)
bool GoplsPlugin::applyEditorBuildTags(LiteApi::IEditor *editor, bool send)
{
if (!isGoEditor(editor)) {
return;
return false;
}
LiteApi::ITextEditor *textEditor = LiteApi::getTextEditor(editor);
if (!textEditor) {
return;
return false;
}
addStandaloneTags(parseStandaloneBuildTags(QString::fromUtf8(textEditor->utf8Data())));
if (!setBuildTags(parseCustomBuildTags(QString::fromUtf8(textEditor->utf8Data())))) {
return false;
}
if (send) {
sendGoplsSettings();
}
return true;
}

void GoplsPlugin::sendGoplsSettings()
Expand All @@ -443,7 +461,7 @@ void GoplsPlugin::sendGoplsSettings()
QJsonObject{{"settings",QJsonObject{{"gopls",goplsSettings()}}}});
}

QStringList GoplsPlugin::parseStandaloneBuildTags(const QString &source) const
QStringList GoplsPlugin::parseCustomBuildTags(const QString &source) const
{
static QSet<QString> knownConstraints;
if (knownConstraints.isEmpty()) {
Expand Down Expand Up @@ -481,8 +499,14 @@ QStringList GoplsPlugin::parseStandaloneBuildTags(const QString &source) const
int pos = 0;
while ((pos = ident.indexIn(expr, pos)) >= 0) {
QString tag = ident.cap(0);
int start = pos;
pos += ident.matchedLength();
if (tag.startsWith("go1") || knownConstraints.contains(tag) || tags.contains(tag)) {
if (start > 0 && expr.at(start-1) == QLatin1Char('!')) {
continue;
}
if (tag == "true" || tag == "false" || tag.startsWith("go1") ||
tag.startsWith("goexperiment") || knownConstraints.contains(tag) ||
tags.contains(tag)) {
continue;
}
tags.append(tag);
Expand All @@ -497,27 +521,22 @@ void GoplsPlugin::handleMissingPackageMetadata(const QString &uri)
if (fileName.isEmpty()) {
return;
}
QString source;
LiteApi::IEditor *editor = m_liteApp->editorManager()->findEditor(fileName,true);
LiteApi::ITextEditor *textEditor = LiteApi::getTextEditor(editor);
if (textEditor) {
source = QString::fromUtf8(textEditor->utf8Data());
} else {
QFile file(fileName);
if (!file.open(QFile::ReadOnly)) {
return;
}
source = QString::fromUtf8(file.readAll());
if (!editor || editor != m_liteApp->editorManager()->currentEditor()) {
return;
}
if (!addStandaloneTags(parseStandaloneBuildTags(source))) {
LiteApi::ITextEditor *textEditor = LiteApi::getTextEditor(editor);
if (!textEditor) {
return;
}
sendGoplsSettings();
if (editor && m_openDocuments.contains(editor)) {
m_client->notify("textDocument/didClose",QJsonObject{{"textDocument",QJsonObject{{"uri",documentUri(editor)}}}});
m_openDocuments.remove(editor);
openDocument(editor);
QStringList tags = parseCustomBuildTags(QString::fromUtf8(textEditor->utf8Data()));
bool changed = setBuildTags(tags);
if (changed) {
sendGoplsSettings();
} else if (m_buildTags.isEmpty()) {
return;
}
reloadDocument(editor);
}

void GoplsPlugin::editorCreated(LiteApi::IEditor *editor)
Expand Down Expand Up @@ -549,6 +568,9 @@ void GoplsPlugin::currentEditorChanged(LiteApi::IEditor *editor)
return;
}
m_workspaceKey = key;
if (m_ready && applyEditorBuildTags(editor,true)) {
reloadDocument(editor);
}
if (m_completer) {
disconnect(m_completer,0,this,0);
m_completer->setSearchSeparator(true);
Expand All @@ -572,9 +594,6 @@ void GoplsPlugin::openDocument(LiteApi::IEditor *editor)
return;
}
QString text = QString::fromUtf8(textEditor->utf8Data());
if (addStandaloneTags(parseStandaloneBuildTags(text))) {
sendGoplsSettings();
}
int version = 1;
m_documentVersions.insert(editor,version);
QJsonObject textDocument;
Expand All @@ -586,6 +605,16 @@ void GoplsPlugin::openDocument(LiteApi::IEditor *editor)
m_openDocuments.insert(editor);
}

void GoplsPlugin::reloadDocument(LiteApi::IEditor *editor)
{
if (!editor || !m_client || !m_client->isRunning() || !m_openDocuments.contains(editor)) {
return;
}
m_client->notify("textDocument/didClose",QJsonObject{{"textDocument",QJsonObject{{"uri",documentUri(editor)}}}});
m_openDocuments.remove(editor);
openDocument(editor);
}

void GoplsPlugin::changeDocument(LiteApi::IEditor *editor)
{
if (!m_openDocuments.contains(editor)) {
Expand Down
8 changes: 5 additions & 3 deletions liteidex/src/plugins/gopls/goplsplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ private slots:
bool isGoEditor(LiteApi::IEditor *editor) const;
QString documentUri(LiteApi::IEditor *editor) const;
void openDocument(LiteApi::IEditor *editor);
void reloadDocument(LiteApi::IEditor *editor);
void changeDocument(LiteApi::IEditor *editor);
void configureCompleter(LiteApi::IEditor *editor, bool enabled);
QJsonObject goplsSettings() const;
QStringList parseStandaloneBuildTags(const QString &source) const;
bool addStandaloneTags(const QStringList &tags);
QStringList parseCustomBuildTags(const QString &source) const;
bool setBuildTags(const QStringList &tags);
bool applyEditorBuildTags(LiteApi::IEditor *editor, bool send);
void resetStandaloneTags();
void collectStandaloneTags(LiteApi::IEditor *editor);
void sendGoplsSettings();
void handleMissingPackageMetadata(const QString &uri);
QString completionKind(int kind) const;
Expand Down Expand Up @@ -124,6 +125,7 @@ private slots:
QString m_restartReason;
QSet<QString> m_metadataWarnings;
QStringList m_standaloneTags;
QStringList m_buildTags;
};

class PluginFactory : public LiteApi::PluginFactoryT<GoplsPlugin>
Expand Down
Loading