Python: Use specialized document to check for pylsp

Change-Id: I9ea13df7fc04a4fe35d901da179b9c0ebaf57bb6
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2022-03-15 13:14:14 +01:00
parent 4a6d6d1323
commit b8c8e0ccae
4 changed files with 20 additions and 18 deletions

View File

@@ -24,9 +24,11 @@
****************************************************************************/ ****************************************************************************/
#include "pythoneditor.h" #include "pythoneditor.h"
#include "pythonconstants.h" #include "pythonconstants.h"
#include "pythonhighlighter.h" #include "pythonhighlighter.h"
#include "pythonindenter.h" #include "pythonindenter.h"
#include "pythonlanguageclient.h"
#include "pythonsettings.h" #include "pythonsettings.h"
#include "pythonutils.h" #include "pythonutils.h"
@@ -102,6 +104,23 @@ static QWidget *createEditorWidget()
return widget; return widget;
} }
class PythonDocument : public TextEditor::TextDocument
{
public:
PythonDocument() : TextEditor::TextDocument(Constants::C_PYTHONEDITOR_ID) {}
void setFilePath(const Utils::FilePath &filePath) override
{
TextEditor::TextDocument::setFilePath(filePath);
const Utils::FilePath &python = detectPython(filePath);
if (!python.exists())
return;
PyLSConfigureAssistant::instance()->openDocumentWithPython(python, this);
}
};
PythonEditorFactory::PythonEditorFactory() PythonEditorFactory::PythonEditorFactory()
{ {
registerReplAction(this); registerReplAction(this);
@@ -116,7 +135,7 @@ PythonEditorFactory::PythonEditorFactory()
| TextEditor::TextEditorActionHandler::UnCollapseAll | TextEditor::TextEditorActionHandler::UnCollapseAll
| TextEditor::TextEditorActionHandler::FollowSymbolUnderCursor); | TextEditor::TextEditorActionHandler::FollowSymbolUnderCursor);
setDocumentCreator([] { return new TextEditor::TextDocument(Constants::C_PYTHONEDITOR_ID); }); setDocumentCreator([]() { return new PythonDocument; });
setEditorWidgetCreator(createEditorWidget); setEditorWidgetCreator(createEditorWidget);
setIndenterCreator([](QTextDocument *doc) { return new PythonIndenter(doc); }); setIndenterCreator([](QTextDocument *doc) { return new PythonIndenter(doc); });
setSyntaxHighlighterCreator([] { return new PythonHighlighter; }); setSyntaxHighlighterCreator([] { return new PythonHighlighter; });

View File

@@ -635,19 +635,6 @@ static void enablePythonLanguageServer(const FilePath &python,
} }
} }
void PyLSConfigureAssistant::documentOpened(Core::IDocument *document)
{
auto textDocument = qobject_cast<TextEditor::TextDocument *>(document);
if (!textDocument || textDocument->mimeType() != Constants::C_PY_MIMETYPE)
return;
const FilePath &python = detectPython(textDocument->filePath());
if (!python.exists())
return;
instance()->openDocumentWithPython(python, textDocument);
}
void PyLSConfigureAssistant::openDocumentWithPython(const FilePath &python, void PyLSConfigureAssistant::openDocumentWithPython(const FilePath &python,
TextEditor::TextDocument *document) TextEditor::TextDocument *document)
{ {

View File

@@ -72,7 +72,6 @@ public:
static const LanguageClient::StdIOSettings *languageServerForPython( static const LanguageClient::StdIOSettings *languageServerForPython(
const Utils::FilePath &python); const Utils::FilePath &python);
static void documentOpened(Core::IDocument *document);
static void updateEditorInfoBars(const Utils::FilePath &python, LanguageClient::Client *client); static void updateEditorInfoBars(const Utils::FilePath &python, LanguageClient::Client *client);
void openDocumentWithPython(const Utils::FilePath &python, TextEditor::TextDocument *document); void openDocumentWithPython(const Utils::FilePath &python, TextEditor::TextDocument *document);

View File

@@ -111,9 +111,6 @@ void PythonPlugin::extensionsInitialized()
Core::FileIconProvider::registerIconOverlayForSuffix(imageFile, "py"); Core::FileIconProvider::registerIconOverlayForSuffix(imageFile, "py");
TaskHub::addCategory(PythonErrorTaskCategory, "Python", true); TaskHub::addCategory(PythonErrorTaskCategory, "Python", true);
connect(Core::EditorManager::instance(), &Core::EditorManager::documentOpened,
this, &PyLSConfigureAssistant::documentOpened);
} }
} // namespace Internal } // namespace Internal