diff options
author | Eike Ziller <[email protected]> | 2024-05-06 16:49:18 +0200 |
---|---|---|
committer | Eike Ziller <[email protected]> | 2024-05-07 10:41:32 +0000 |
commit | 770f1b0376db49b9ea5aad906dd6165ab71d9152 (patch) | |
tree | 82550a2b183ac1dca16ee9716062302a08a9a66a /src/plugins/languageclient | |
parent | 67072d3f5bb1b425a2b9d3bf30d57542e9f88902 (diff) |
LanguageClient: Avoid calling throwing functions
Similar to with std::optional we want to avoid calling throwing
functions for std::variant, which includes std::get.
In many cases this also avoids the chain of `std::holds_alternative` +
`std::get` calls. And we never declare any `throws`.
Change-Id: I14f62ddef921b6bee90226ea34d1ffa62629bdc3
Reviewed-by: David Schulz <[email protected]>
Reviewed-by: <[email protected]>
Diffstat (limited to 'src/plugins/languageclient')
10 files changed, 89 insertions, 100 deletions
diff --git a/src/plugins/languageclient/client.cpp b/src/plugins/languageclient/client.cpp index 0d7826307ab..f56538d6cbe 100644 --- a/src/plugins/languageclient/client.cpp +++ b/src/plugins/languageclient/client.cpp @@ -903,7 +903,8 @@ void ClientPrivate::requestDocumentHighlightsNow(TextEditor::TextEditorWidget *w = m_serverCapabilities.documentHighlightProvider(); if (!provider.has_value()) return; - if (std::holds_alternative<bool>(*provider) && !std::get<bool>(*provider)) + const auto boolvalue = std::get_if<bool>(&*provider); + if (boolvalue && !*boolvalue) return; } @@ -935,7 +936,8 @@ void ClientPrivate::requestDocumentHighlightsNow(TextEditor::TextEditorWidget *w const QTextCharFormat &format = widget->textDocument()->fontSettings().toTextCharFormat(TextEditor::C_OCCURRENCES); QTextDocument *document = widget->document(); - for (const auto &highlight : std::get<QList<DocumentHighlight>>(*result)) { + const auto highlights = std::get_if<QList<DocumentHighlight>>(&*result); + for (const auto &highlight : *highlights) { QTextEdit::ExtraSelection selection{widget->textCursor(), format}; const int &start = highlight.range().start().toPositionInDocument(document); const int &end = highlight.range().end().toPositionInDocument(document); @@ -1435,7 +1437,8 @@ void Client::requestCodeActions(const CodeActionRequest &request) } else { std::variant<bool, CodeActionOptions> provider = d->m_serverCapabilities.codeActionProvider().value_or(false); - if (!(std::holds_alternative<CodeActionOptions>(provider) || std::get<bool>(provider))) + const auto boolvalue = std::get_if<bool>(&provider); + if (boolvalue && !*boolvalue) return; } @@ -1661,8 +1664,8 @@ bool Client::supportsDocumentSymbols(const TextEditor::TextDocument *doc) const = capabilities().documentSymbolProvider(); if (!provider.has_value()) return false; - if (std::holds_alternative<bool>(*provider)) - return std::get<bool>(*provider); + if (const auto boolvalue = std::get_if<bool>(&*provider)) + return *boolvalue; return true; } @@ -2233,10 +2236,10 @@ bool ClientPrivate::sendWorkspceFolderChanges() const if (auto folder = workspace->workspaceFolders()) { if (folder->supported().value_or(false)) { // holds either the Id for deregistration or whether it is registered - auto notification = folder->changeNotifications().value_or(false); - return std::holds_alternative<QString>(notification) - || (std::holds_alternative<bool>(notification) - && std::get<bool>(notification)); + const std::variant<QString, bool> notification + = folder->changeNotifications().value_or(false); + const auto boolvalue = std::get_if<bool>(¬ification); + return !boolvalue || *boolvalue; } } } diff --git a/src/plugins/languageclient/clientrequest.cpp b/src/plugins/languageclient/clientrequest.cpp index 4e221f1b9d4..3125b25a72c 100644 --- a/src/plugins/languageclient/clientrequest.cpp +++ b/src/plugins/languageclient/clientrequest.cpp @@ -29,7 +29,8 @@ bool ClientWorkspaceSymbolRequest::preStartCheck() = client()->capabilities().workspaceSymbolProvider(); if (!capability.has_value()) return false; - if (std::holds_alternative<bool>(*capability) && !std::get<bool>(*capability)) + const auto boolvalue = std::get_if<bool>(&*capability); + if (boolvalue && !boolvalue) return false; return true; diff --git a/src/plugins/languageclient/languageclientcompletionassist.cpp b/src/plugins/languageclient/languageclientcompletionassist.cpp index d6a2904fa4c..7486ca102c7 100644 --- a/src/plugins/languageclient/languageclientcompletionassist.cpp +++ b/src/plugins/languageclient/languageclientcompletionassist.cpp @@ -123,11 +123,11 @@ QString LanguageClientCompletionItem::detail() const if (auto _doc = m_item.documentation()) { auto doc = *_doc; QString detailDocText; - if (std::holds_alternative<QString>(doc)) { - detailDocText = std::get<QString>(doc); - } else if (std::holds_alternative<MarkupContent>(doc)) { + if (const auto s = std::get_if<QString>(&doc)) { + detailDocText = *s; + } else if (const auto m = std::get_if<MarkupContent>(&doc)) { // TODO markdown parser? - detailDocText = std::get<MarkupContent>(doc).content(); + detailDocText = m->content(); } if (!detailDocText.isEmpty()) return detailDocText; @@ -495,12 +495,10 @@ void LanguageClientCompletionAssistProcessor::handleCompletionResponse( } QList<CompletionItem> items; - if (std::holds_alternative<CompletionList>(*result)) { - const auto &list = std::get<CompletionList>(*result); - items = list.items().value_or(QList<CompletionItem>()); - } else if (std::holds_alternative<QList<CompletionItem>>(*result)) { - items = std::get<QList<CompletionItem>>(*result); - } + if (const auto list = std::get_if<CompletionList>(&*result)) + items = list->items().value_or(QList<CompletionItem>()); + else if (const auto l = std::get_if<QList<CompletionItem>>(&*result)) + items = *l; auto proposalItems = generateCompletionItems(items); if (!m_snippetsGroup.isEmpty()) { proposalItems << TextEditor::SnippetAssistCollector(m_snippetsGroup, diff --git a/src/plugins/languageclient/languageclientformatter.cpp b/src/plugins/languageclient/languageclientformatter.cpp index 52f35856af3..acebc3184ba 100644 --- a/src/plugins/languageclient/languageclientformatter.cpp +++ b/src/plugins/languageclient/languageclientformatter.cpp @@ -69,7 +69,8 @@ QFutureWatcher<ChangeSet> *LanguageClientFormatter::format( = m_client->capabilities().documentRangeFormattingProvider(); if (!provider.has_value()) return nullptr; - if (std::holds_alternative<bool>(*provider) && !std::get<bool>(*provider)) + const auto boolvalue = std::get_if<bool>(&*provider); + if (boolvalue && !*boolvalue) return nullptr; } DocumentRangeFormattingParams params; diff --git a/src/plugins/languageclient/languageclienthoverhandler.cpp b/src/plugins/languageclient/languageclienthoverhandler.cpp index e878a62749b..429c89e511a 100644 --- a/src/plugins/languageclient/languageclienthoverhandler.cpp +++ b/src/plugins/languageclient/languageclienthoverhandler.cpp @@ -87,9 +87,8 @@ void HoverHandler::identifyMatch(TextEditor::TextEditorWidget *editorWidget, const std::optional<std::variant<bool, WorkDoneProgressOptions>> &provider = m_client->capabilities().hoverProvider(); - bool sendMessage = provider.has_value(); - if (sendMessage && std::holds_alternative<bool>(*provider)) - sendMessage = std::get<bool>(*provider); + const bool *boolvalue = provider.has_value() ? std::get_if<bool>(&*provider) : nullptr; + bool sendMessage = provider.has_value() && (!boolvalue || *boolvalue); if (std::optional<bool> registered = m_client->dynamicCapabilities().isRegistered( HoverRequest::methodName)) { sendMessage = *registered; diff --git a/src/plugins/languageclient/languageclientoutline.cpp b/src/plugins/languageclient/languageclientoutline.cpp index 8c45513ce9f..504c9157599 100644 --- a/src/plugins/languageclient/languageclientoutline.cpp +++ b/src/plugins/languageclient/languageclientoutline.cpp @@ -228,10 +228,10 @@ void LanguageClientOutlineWidget::handleResponse(const DocumentUri &uri, { if (uri != m_uri) return; - if (std::holds_alternative<QList<SymbolInformation>>(result)) - m_model.setInfo(std::get<QList<SymbolInformation>>(result)); - else if (std::holds_alternative<QList<DocumentSymbol>>(result)) - m_model.setInfo(std::get<QList<DocumentSymbol>>(result)); + if (const auto i = std::get_if<QList<SymbolInformation>>(&result)) + m_model.setInfo(*i); + else if (const auto s = std::get_if<QList<DocumentSymbol>>(&result)) + m_model.setInfo(*s); else m_model.clear(); @@ -369,10 +369,10 @@ void OutlineComboBox::updateModel(const DocumentUri &resultUri, const DocumentSy { if (m_uri != resultUri) return; - if (std::holds_alternative<QList<SymbolInformation>>(result)) - m_model.setInfo(std::get<QList<SymbolInformation>>(result)); - else if (std::holds_alternative<QList<DocumentSymbol>>(result)) - m_model.setInfo(std::get<QList<DocumentSymbol>>(result)); + if (const auto i = std::get_if<QList<SymbolInformation>>(&result)) + m_model.setInfo(*i); + else if (const auto s = std::get_if<QList<DocumentSymbol>>(&result)) + m_model.setInfo(*i); else m_model.clear(); diff --git a/src/plugins/languageclient/languageclientsymbolsupport.cpp b/src/plugins/languageclient/languageclientsymbolsupport.cpp index 9bc7cd1027f..71b41bbcbfa 100644 --- a/src/plugins/languageclient/languageclientsymbolsupport.cpp +++ b/src/plugins/languageclient/languageclientsymbolsupport.cpp @@ -100,9 +100,8 @@ static MessageId sendTextDocumentPositionParamsRequest(Client *client, sendMessage = supportedFile; } else { const auto provider = std::mem_fn(member)(serverCapability); - sendMessage = provider.has_value(); - if (sendMessage && std::holds_alternative<bool>(*provider)) - sendMessage = std::get<bool>(*provider); + const bool *boolvalue = provider.has_value() ? std::get_if<bool>(&*provider) : nullptr; + sendMessage = provider.has_value() && (!boolvalue || *boolvalue); } if (sendMessage) { client->sendMessage(request); @@ -191,9 +190,8 @@ bool SymbolSupport::supportsFindLink(TextEditor::TextDocument *document, LinkTar else supported = m_client->isSupportedUri(uri); } else { - supported = provider.has_value(); - if (supported && std::holds_alternative<bool>(*provider)) - supported = std::get<bool>(*provider); + const bool *boolvalue = provider.has_value() ? std::get_if<bool>(&*provider) : nullptr; + supported = provider.has_value() && (!boolvalue || *boolvalue); } return supported; } @@ -259,8 +257,8 @@ bool SymbolSupport::supportsFindUsages(TextEditor::TextDocument *document) const return false; } } else if (auto referencesProvider = m_client->capabilities().referencesProvider()) { - if (std::holds_alternative<bool>(*referencesProvider)) { - if (!std::get<bool>(*referencesProvider)) + if (const auto b = std::get_if<bool>(&*referencesProvider)) { + if (!*b) return false; } } else { @@ -447,13 +445,11 @@ static bool supportsRename(Client *client, } } if (auto renameProvider = client->capabilities().renameProvider()) { - if (std::holds_alternative<bool>(*renameProvider)) { - if (!std::get<bool>(*renameProvider)) + if (const auto b = std::get_if<bool>(&*renameProvider)) { + if (!*b) return false; - } else if (std::holds_alternative<ServerCapabilities::RenameOptions>(*renameProvider)) { - prepareSupported = std::get<ServerCapabilities::RenameOptions>(*renameProvider) - .prepareProvider() - .value_or(false); + } else if (const auto opt = std::get_if<ServerCapabilities::RenameOptions>(&*renameProvider)) { + prepareSupported = opt->prepareProvider().value_or(false); } } else { return false; @@ -524,19 +520,17 @@ void SymbolSupport::requestPrepareRename(TextEditor::TextDocument *document, const std::optional<PrepareRenameResult> &result = response.result(); if (result.has_value()) { - if (std::holds_alternative<PlaceHolderResult>(*result)) { - auto placeHolderResult = std::get<PlaceHolderResult>(*result); - startRenameSymbol(params, - placeholder.isEmpty() ? placeHolderResult.placeHolder() - : placeholder, - oldSymbolName, - callback, - preferLowerCaseFileNames); - } else if (std::holds_alternative<Range>(*result)) { - auto range = std::get<Range>(*result); + if (const auto placeHolderResult = std::get_if<PlaceHolderResult>(&*result)) { + startRenameSymbol( + params, + placeholder.isEmpty() ? placeHolderResult->placeHolder() : placeholder, + oldSymbolName, + callback, + preferLowerCaseFileNames); + } else if (const auto range = std::get_if<Range>(&*result)) { if (document) { - const int start = range.start().toPositionInDocument(document->document()); - const int end = range.end().toPositionInDocument(document->document()); + const int start = range->start().toPositionInDocument(document->document()); + const int end = range->end().toPositionInDocument(document->document()); const QString reportedSymbolName = document->textAt(start, end - start); startRenameSymbol(params, derivePlaceholder(reportedSymbolName, placeholder), @@ -586,28 +580,24 @@ Utils::SearchResultItems generateReplaceItems(const WorkspaceEdit &edits, const DocumentUri::PathMapper &pathMapper = client->hostPathMapper(); if (!documentChanges.isEmpty()) { for (const DocumentChange &documentChange : std::as_const(documentChanges)) { - if (std::holds_alternative<TextDocumentEdit>(documentChange)) { - const TextDocumentEdit edit = std::get<TextDocumentEdit>(documentChange); - rangesInDocument[edit.textDocument().uri().toFilePath(pathMapper)] = convertEdits( - edit.edits()); + if (const auto edit = std::get_if<TextDocumentEdit>(&documentChange)) { + rangesInDocument[edit->textDocument().uri().toFilePath(pathMapper)] = convertEdits( + edit->edits()); } else { Utils::SearchResultItem item; - if (std::holds_alternative<CreateFileOperation>(documentChange)) { - auto op = std::get<CreateFileOperation>(documentChange); - item.setLineText(op.message(pathMapper)); - item.setFilePath(op.uri().toFilePath(pathMapper)); - item.setUserData(QVariant(op)); - } else if (std::holds_alternative<RenameFileOperation>(documentChange)) { - auto op = std::get<RenameFileOperation>(documentChange); - item.setLineText(op.message(pathMapper)); - item.setFilePath(op.oldUri().toFilePath(pathMapper)); - item.setUserData(QVariant(op)); - } else if (std::holds_alternative<DeleteFileOperation>(documentChange)) { - auto op = std::get<DeleteFileOperation>(documentChange); - item.setLineText(op.message(pathMapper)); - item.setFilePath(op.uri().toFilePath(pathMapper)); - item.setUserData(QVariant(op)); + if (const auto op = std::get_if<CreateFileOperation>(&documentChange)) { + item.setLineText(op->message(pathMapper)); + item.setFilePath(op->uri().toFilePath(pathMapper)); + item.setUserData(QVariant(*op)); + } else if (const auto op = std::get_if<RenameFileOperation>(&documentChange)) { + item.setLineText(op->message(pathMapper)); + item.setFilePath(op->oldUri().toFilePath(pathMapper)); + item.setUserData(QVariant(*op)); + } else if (const auto op = std::get_if<DeleteFileOperation>(&documentChange)) { + item.setLineText(op->message(pathMapper)); + item.setFilePath(op->uri().toFilePath(pathMapper)); + item.setUserData(QVariant(*op)); } items << item; diff --git a/src/plugins/languageclient/languageclientutils.cpp b/src/plugins/languageclient/languageclientutils.cpp index 3df7622548d..4924654ffc9 100644 --- a/src/plugins/languageclient/languageclientutils.cpp +++ b/src/plugins/languageclient/languageclientutils.cpp @@ -349,13 +349,12 @@ bool applyDocumentChange(const Client *client, const DocumentChange &change) if (!client) return false; - if (std::holds_alternative<TextDocumentEdit>(change)) { - return applyTextDocumentEdit(client, std::get<TextDocumentEdit>(change)); - } else if (std::holds_alternative<CreateFileOperation>(change)) { - const auto createOperation = std::get<CreateFileOperation>(change); - const FilePath filePath = createOperation.uri().toFilePath(client->hostPathMapper()); + if (const auto e = std::get_if<TextDocumentEdit>(&change)) { + return applyTextDocumentEdit(client, *e); + } else if (const auto createOperation = std::get_if<CreateFileOperation>(&change)) { + const FilePath filePath = createOperation->uri().toFilePath(client->hostPathMapper()); if (filePath.exists()) { - if (const std::optional<CreateFileOptions> options = createOperation.options()) { + if (const std::optional<CreateFileOptions> options = createOperation->options()) { if (options->overwrite().value_or(false)) { if (!filePath.removeFile()) return false; @@ -365,16 +364,15 @@ bool applyDocumentChange(const Client *client, const DocumentChange &change) } } return filePath.ensureExistingFile(); - } else if (std::holds_alternative<RenameFileOperation>(change)) { - const RenameFileOperation renameOperation = std::get<RenameFileOperation>(change); - const FilePath oldPath = renameOperation.oldUri().toFilePath(client->hostPathMapper()); + } else if (const auto renameOperation = std::get_if<RenameFileOperation>(&change)) { + const FilePath oldPath = renameOperation->oldUri().toFilePath(client->hostPathMapper()); if (!oldPath.exists()) return false; - const FilePath newPath = renameOperation.newUri().toFilePath(client->hostPathMapper()); + const FilePath newPath = renameOperation->newUri().toFilePath(client->hostPathMapper()); if (oldPath == newPath) return true; if (newPath.exists()) { - if (const std::optional<CreateFileOptions> options = renameOperation.options()) { + if (const std::optional<CreateFileOptions> options = renameOperation->options()) { if (options->overwrite().value_or(false)) { if (!newPath.removeFile()) return false; @@ -384,10 +382,9 @@ bool applyDocumentChange(const Client *client, const DocumentChange &change) } } return oldPath.renameFile(newPath); - } else if (std::holds_alternative<DeleteFileOperation>(change)) { - const auto deleteOperation = std::get<DeleteFileOperation>(change); - const FilePath filePath = deleteOperation.uri().toFilePath(client->hostPathMapper()); - if (const std::optional<DeleteFileOptions> options = deleteOperation.options()) { + } else if (const auto deleteOperation = std::get_if<DeleteFileOperation>(&change)) { + const FilePath filePath = deleteOperation->uri().toFilePath(client->hostPathMapper()); + if (const std::optional<DeleteFileOptions> options = deleteOperation->options()) { if (!filePath.exists()) return options->ignoreIfNotExists().value_or(false); if (filePath.isDir() && options->recursive().value_or(false)) diff --git a/src/plugins/languageclient/progressmanager.cpp b/src/plugins/languageclient/progressmanager.cpp index 6b82d289dfc..4ef7d0b60d5 100644 --- a/src/plugins/languageclient/progressmanager.cpp +++ b/src/plugins/languageclient/progressmanager.cpp @@ -69,10 +69,10 @@ bool ProgressManager::isProgressEndMessage(const LanguageServerProtocol::Progres Utils::Id languageClientProgressId(const ProgressToken &token) { constexpr char k_LanguageClientProgressId[] = "LanguageClient.ProgressId."; - auto toString = [](const ProgressToken &token){ - if (std::holds_alternative<int>(token)) - return QString::number(std::get<int>(token)); - return std::get<QString>(token); + auto toString = [](const ProgressToken &token) { + if (const auto i = std::get_if<int>(&token)) + return QString::number(*i); + return *std::get_if<QString>(&token); }; return Utils::Id(k_LanguageClientProgressId).withSuffix(toString(token)); } diff --git a/src/plugins/languageclient/snippet.cpp b/src/plugins/languageclient/snippet.cpp index 7a0fd211316..b401123ef86 100644 --- a/src/plugins/languageclient/snippet.cpp +++ b/src/plugins/languageclient/snippet.cpp @@ -251,11 +251,11 @@ void SnippetParsingTest::testSnippetParsing() QFETCH(Parts, parts); SnippetParseResult result = LanguageClient::parseSnippet(input); - QCOMPARE(std::holds_alternative<ParsedSnippet>(result), success); + const auto snippet = std::get_if<ParsedSnippet>(&result); + QCOMPARE(snippet != nullptr, success); if (!success) return; - ParsedSnippet snippet = std::get<ParsedSnippet>(result); auto rangesCompare = [&](const ParsedSnippet::Part &actual, const SnippetPart &expected) { QCOMPARE(actual.text, expected.text); @@ -264,10 +264,10 @@ void SnippetParsingTest::testSnippetParsing() QCOMPARE(manglerId, expected.manglerId); }; - QCOMPARE(snippet.parts.count(), parts.count()); + QCOMPARE(snippet->parts.count(), parts.count()); for (int i = 0; i < parts.count(); ++i) - rangesCompare(snippet.parts.at(i), parts.at(i)); + rangesCompare(snippet->parts.at(i), parts.at(i)); } QObject *createSnippetParsingTest() |