MCPcopy Create free account
hub / github.com/NPP-JSONViewer/JSON-Viewer / SearchInTree

Method SearchInTree

src/NppJsonViewer/JsonViewDlg.cpp:486–566  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

484}
485
486void JsonViewDlg::SearchInTree()
487{
488 std::wstring itemToSearch = CUtility::GetEditCtrlText(::GetDlgItem(_hSelf, IDC_EDT_SEARCH));
489 CUtility::SetEditCtrlText(::GetDlgItem(_hSelf, IDC_EDT_NODEPATH), STR_SRCH_SEARCHING + itemToSearch);
490 m_pTreeView->SetSelection(nullptr);
491
492 static int foundCount = 0;
493 static std::wstring previousSearch;
494 static HTREEITEM nextNode = m_pTreeView->NextItem(m_pTreeView->GetRoot());
495
496 // New search, hence search from beginning
497 if (previousSearch != itemToSearch)
498 {
499 previousSearch = itemToSearch;
500 nextNode = m_pTreeView->NextItem(m_pTreeView->GetRoot());
501 foundCount = 0;
502 }
503 else
504 {
505 nextNode = m_pTreeView->NextItem(nextNode);
506 if (nextNode == m_pTreeView->GetRoot())
507 {
508 nextNode = m_pTreeView->NextItem(nextNode);
509 foundCount = 0;
510 }
511 }
512
513 // Check if this is an empty json
514 std::wstring nodeText = m_pTreeView->GetNodeName(nextNode, true);
515 if (nodeText.empty() || wcscmp(nodeText.c_str(), JSON_ERR_PARSE) == 0)
516 {
517 CUtility::SetEditCtrlText(::GetDlgItem(_hSelf, IDC_EDT_NODEPATH), STR_SRCH_NOTFOUND + itemToSearch);
518 }
519 else
520 {
521 bool bFound = false;
522 while (!bFound && nextNode)
523 {
524 nodeText = m_pTreeView->GetNodeName(nextNode, true);
525 auto nodeKey = m_pTreeView->GetNodeKey(nextNode);
526 auto nodeVal = m_pTreeView->GetNodeValue(nextNode);
527
528 // Search in node value
529 // 1. If both key and value are not equal
530 // 2. If both are equal, but not all three (key, value and keyValue)
531 // 3. If all three equal, but key does not start with '[' and end with ']'
532
533 bool shouldSearch = (nodeKey != nodeVal);
534 shouldSearch |= (nodeKey == nodeVal && nodeKey != nodeText);
535 shouldSearch |= (nodeKey == nodeVal && nodeKey == nodeText && !nodeKey.starts_with(L"[") && !nodeKey.ends_with(L"]"));
536 if (shouldSearch)
537 bFound = StringHelper::Contains(nodeVal, itemToSearch);
538
539 // Search in Key if not found in value
540 // 1. If key does not start with '[' and end with ']'
541
542 shouldSearch = (!nodeKey.starts_with(L"[") && !nodeKey.ends_with(L"]"));
543 if (!bFound && shouldSearch)

Callers

nothing calls this directly

Calls 6

SetSelectionMethod · 0.80
GetRootMethod · 0.80
GetNodeNameMethod · 0.80
GetNodeKeyMethod · 0.80
GetNodeValueMethod · 0.80
NextItemMethod · 0.45

Tested by

no test coverage detected