sw/qa/uibase/uno/uno.cxx          |   35 +++++++++++++++++++++
 sw/source/uibase/uno/loktxdoc.cxx |   62 ++++++++++++++++++++++++++++++++++++--
 2 files changed, 95 insertions(+), 2 deletions(-)

New commits:
commit 81f690ec0cb2a6dc0d6ca0f6de3adcc07eb7bc12
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Thu Jan 12 15:13:00 2023 +0100
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Fri Jan 13 07:13:00 2023 +0000

    sw lok: expose name of refmark under cursor
    
    This is similar to commit 4bcb66ec7b417fbe113267f2615e78fe47eb55ca (sw
    lok: expose name of bookmark under cursor, 2023-01-11), but that was for
    the bookmark under the cursor and this is for refmarks.
    
    Change-Id: I0acf0181d0acfdc087e1ed737b3c18ab3736031a
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145405
    Tested-by: Jenkins
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>

diff --git a/sw/qa/uibase/uno/uno.cxx b/sw/qa/uibase/uno/uno.cxx
index d2be66457b81..7a1b731634bd 100644
--- a/sw/qa/uibase/uno/uno.cxx
+++ b/sw/qa/uibase/uno/uno.cxx
@@ -475,6 +475,41 @@ CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testGetBookmark)
     CPPUNIT_ASSERT_EQUAL(std::string("ZOTERO_BREF_1"), 
aBookmark.get<std::string>("name"));
 }
 
+CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testGetField)
+{
+    // Given a document with a refmark:
+    createSwDoc();
+    uno::Sequence<css::beans::PropertyValue> aArgs = {
+        comphelper::makePropertyValue("TypeName", 
uno::Any(OUString("SetRef"))),
+        comphelper::makePropertyValue("Name",
+                                      uno::Any(OUString("ZOTERO_ITEM 
CSL_CITATION {} refmark"))),
+        comphelper::makePropertyValue("Content", 
uno::Any(OUString("content"))),
+    };
+    dispatchCommand(mxComponent, ".uno:InsertField", aArgs);
+
+    // When in the refmark with the cursor and getting the command value for 
.uno:Field:
+    SwDoc* pDoc = getSwDoc();
+    SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
+    pWrtShell->SttEndDoc(/*bStt=*/false);
+    pWrtShell->Left(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, 
/*bBasicCall=*/false);
+    tools::JsonWriter aJsonWriter;
+    std::string_view 
aCommand(".uno:Field?typeName=SetRef&namePrefix=ZOTERO_ITEM%20CSL_CITATION");
+    auto pXTextDocument = dynamic_cast<SwXTextDocument*>(mxComponent.get());
+    pXTextDocument->getCommandValues(aJsonWriter, aCommand);
+
+    // Then make sure we find the inserted refmark:
+    std::unique_ptr<char[], o3tl::free_delete> 
pJSON(aJsonWriter.extractData());
+    std::stringstream aStream(pJSON.get());
+    boost::property_tree::ptree aTree;
+    boost::property_tree::read_json(aStream, aTree);
+    boost::property_tree::ptree aBookmark = aTree.get_child("setRef");
+    // Without the accompanying fix in place, this test would have failed with:
+    // - No such node (setRef)
+    // i.e. the returned JSON was an empty object.
+    CPPUNIT_ASSERT_EQUAL(std::string("ZOTERO_ITEM CSL_CITATION {} refmark"),
+                         aBookmark.get<std::string>("name"));
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/uibase/uno/loktxdoc.cxx 
b/sw/source/uibase/uno/loktxdoc.cxx
index 44b72d4f1eae..6742c819c899 100644
--- a/sw/source/uibase/uno/loktxdoc.cxx
+++ b/sw/source/uibase/uno/loktxdoc.cxx
@@ -326,6 +326,58 @@ void GetFields(tools::JsonWriter& rJsonWriter, SwDocShell* 
pDocShell,
     }
 }
 
+/// Implements getCommandValues(".uno:Field").
+///
+/// Parameters:
+///
+/// - typeName: field type condition to not return all fields
+/// - namePrefix: field name prefix to not return all fields
+void GetField(tools::JsonWriter& rJsonWriter, SwDocShell* pDocShell,
+              const std::map<OUString, OUString>& rArguments)
+{
+    OUString aTypeName;
+    {
+        auto it = rArguments.find("typeName");
+        if (it != rArguments.end())
+        {
+            aTypeName = it->second;
+        }
+    }
+    // See SwFieldTypeFromString().
+    if (aTypeName != "SetRef")
+    {
+        return;
+    }
+
+    OUString aNamePrefix;
+    {
+        auto it = rArguments.find("namePrefix");
+        if (it != rArguments.end())
+        {
+            aNamePrefix = it->second;
+        }
+    }
+
+    SwWrtShell* pWrtShell = pDocShell->GetWrtShell();
+    SwPosition& rCursor = *pWrtShell->GetCursor()->GetPoint();
+    SwTextNode* pTextNode = rCursor.GetNode().GetTextNode();
+    std::vector<SwTextAttr*> aAttrs
+        = pTextNode->GetTextAttrsAt(rCursor.GetContentIndex(), 
RES_TXTATR_REFMARK);
+    tools::ScopedJsonWriterNode aRefmark = rJsonWriter.startNode("setRef");
+    if (aAttrs.empty())
+    {
+        return;
+    }
+
+    const SwFormatRefMark& rRefmark = aAttrs[0]->GetRefMark();
+    if (!rRefmark.GetRefName().startsWith(aNamePrefix))
+    {
+        return;
+    }
+
+    rJsonWriter.put("name", rRefmark.GetRefName());
+}
+
 /// Implements getCommandValues(".uno:Sections").
 ///
 /// Parameters:
@@ -361,8 +413,9 @@ void GetSections(tools::JsonWriter& rJsonWriter, 
SwDocShell* pDocShell,
 bool SwXTextDocument::supportsCommand(std::u16string_view rCommand)
 {
     static const std::initializer_list<std::u16string_view> vForward
-        = { u"TextFormFields", u"TextFormField", u"SetDocumentProperties", 
u"Bookmarks", u"Fields",
-            u"Sections",       u"Bookmark" };
+        = { u"TextFormFields", u"TextFormField", u"SetDocumentProperties",
+            u"Bookmarks",      u"Fields",        u"Sections",
+            u"Bookmark",       u"Field" };
 
     return std::find(vForward.begin(), vForward.end(), rCommand) != 
vForward.end();
 }
@@ -378,6 +431,7 @@ void SwXTextDocument::getCommandValues(tools::JsonWriter& 
rJsonWriter, std::stri
     static constexpr OStringLiteral aFields(".uno:Fields");
     static constexpr OStringLiteral aSections(".uno:Sections");
     static constexpr OStringLiteral aBookmark(".uno:Bookmark");
+    static constexpr OStringLiteral aField(".uno:Field");
 
     INetURLObject aParser(OUString::fromUtf8(rCommand));
     OUString aArguments = aParser.GetParam();
@@ -429,6 +483,10 @@ void SwXTextDocument::getCommandValues(tools::JsonWriter& 
rJsonWriter, std::stri
     {
         GetBookmark(rJsonWriter, m_pDocShell, aMap);
     }
+    else if (o3tl::starts_with(rCommand, aField))
+    {
+        GetField(rJsonWriter, m_pDocShell, aMap);
+    }
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Reply via email to