| 1756 | } |
| 1757 | |
| 1758 | void format(QChar lastChar = QChar(), bool save = false) |
| 1759 | { |
| 1760 | KTextEditor::View *activeView = m_mainWindow->activeView(); |
| 1761 | QPointer<KTextEditor::Document> document = activeView ? activeView->document() : nullptr; |
| 1762 | auto server = m_serverManager->findServer(activeView); |
| 1763 | if (!server || !document) { |
| 1764 | return; |
| 1765 | } |
| 1766 | |
| 1767 | int tabSize = 4; |
| 1768 | bool insertSpaces = true; |
| 1769 | tabSize = document->configValue(QStringLiteral("tab-width")).toInt(); |
| 1770 | insertSpaces = document->configValue(QStringLiteral("replace-tabs")).toBool(); |
| 1771 | |
| 1772 | // sigh, no move initialization capture ... |
| 1773 | // (again) assuming reply ranges wrt revisions submitted at this time |
| 1774 | std::shared_ptr<LSPClientRevisionSnapshot> snapshot(m_serverManager->snapshot(server.get())); |
| 1775 | auto h = [this, document, snapshot, lastChar, save](const QList<LSPTextEdit> &edits) { |
| 1776 | if (lastChar.isNull()) { |
| 1777 | checkEditResult(edits); |
| 1778 | } |
| 1779 | if (document) { |
| 1780 | // Must clear formatting triggers here otherwise on applying edits we |
| 1781 | // might end up triggering formatting again ending up in an infinite loop |
| 1782 | auto savedTriggers = m_onTypeFormattingTriggers; |
| 1783 | m_onTypeFormattingTriggers.clear(); |
| 1784 | applyEdits(document, snapshot.get(), edits); |
| 1785 | m_onTypeFormattingTriggers = savedTriggers; |
| 1786 | if (save) { |
| 1787 | QObject::disconnect(m_formatOnSaveConnection); |
| 1788 | document->documentSave(); |
| 1789 | m_formatOnSaveConnection = |
| 1790 | connect(document, &KTextEditor::Document::documentSavedOrUploaded, this, &self_type::formatOnSave, Qt::QueuedConnection); |
| 1791 | } |
| 1792 | } |
| 1793 | }; |
| 1794 | |
| 1795 | auto options = LSPFormattingOptions{.tabSize = tabSize, .insertSpaces = insertSpaces, .extra = QJsonObject()}; |
| 1796 | auto handle = !lastChar.isNull() |
| 1797 | ? server->documentOnTypeFormatting(document->url(), activeView->cursorPosition(), lastChar, options, this, h) |
| 1798 | : (activeView->selection() ? server->documentRangeFormatting(document->url(), activeView->selectionRange(), options, this, h) |
| 1799 | : server->documentFormatting(document->url(), options, this, h)); |
| 1800 | delayCancelRequest(std::move(handle)); |
| 1801 | } |
| 1802 | |
| 1803 | void rename() |
| 1804 | { |
nothing calls this directly
no test coverage detected