From 6fe90532dcf9929472dbe2cbbe76ea38bbadc1ac Mon Sep 17 00:00:00 2001 From: Tim Jenssen Date: Wed, 23 Nov 2016 13:31:47 +0100 Subject: [PATCH] Utils: Add offset to search results Offset are better to compute the length of a text range. The only problem is that we get them for utf8 because that is the text representation of source file. QTextDocument is Utf16 based, so it can not binary represent the source file. Actually I do not see a simple performing workaround for it. Change-Id: Id615e1ee6a6e85c6ecc4f8044e275142409d9b46 Reviewed-by: Tim Jenssen --- .../qtcreatorsearchhandle.cpp | 2 +- .../clangrefactoring/qtcreatorsearchhandle.h | 2 +- .../clangrefactoring/refactoringclient.cpp | 6 +- .../coreplugin/find/searchresultitem.h | 14 ++++- tests/unit/unittest/compare-operators.h | 48 ++++++++++++++++ tests/unit/unittest/googletest.h | 3 + .../unit/unittest/gtest-creator-printing.cpp | 56 +++++++++++++++++++ tests/unit/unittest/gtest-creator-printing.h | 42 ++++++++++++++ .../unit/unittest/refactoringclient-test.cpp | 12 ++++ tests/unit/unittest/unittest.pro | 5 +- 10 files changed, 184 insertions(+), 6 deletions(-) create mode 100644 tests/unit/unittest/compare-operators.h create mode 100644 tests/unit/unittest/gtest-creator-printing.cpp create mode 100644 tests/unit/unittest/gtest-creator-printing.h diff --git a/src/plugins/clangrefactoring/qtcreatorsearchhandle.cpp b/src/plugins/clangrefactoring/qtcreatorsearchhandle.cpp index 6308c641ece..2364931ce60 100644 --- a/src/plugins/clangrefactoring/qtcreatorsearchhandle.cpp +++ b/src/plugins/clangrefactoring/qtcreatorsearchhandle.cpp @@ -40,7 +40,7 @@ QtCreatorSearchHandle::QtCreatorSearchHandle(Core::SearchResult *searchResult) void QtCreatorSearchHandle::addResult(const QString &fileName, const QString &lineText, - Core::TextRange textRange) + Core::Search::TextRange textRange) { searchResult->addResult(fileName, lineText, textRange); } diff --git a/src/plugins/clangrefactoring/qtcreatorsearchhandle.h b/src/plugins/clangrefactoring/qtcreatorsearchhandle.h index 76932edb1f4..e0dc80a0c6f 100644 --- a/src/plugins/clangrefactoring/qtcreatorsearchhandle.h +++ b/src/plugins/clangrefactoring/qtcreatorsearchhandle.h @@ -40,7 +40,7 @@ public: void addResult(const QString &fileName, const QString &lineText, - Core::TextRange textRange) override; + Core::Search::TextRange textRange) override; void setExpectedResultCount(uint count) override; void setResultCounter(uint counter) override; diff --git a/src/plugins/clangrefactoring/refactoringclient.cpp b/src/plugins/clangrefactoring/refactoringclient.cpp index da0701c2f59..4fabb1759cb 100644 --- a/src/plugins/clangrefactoring/refactoringclient.cpp +++ b/src/plugins/clangrefactoring/refactoringclient.cpp @@ -143,9 +143,11 @@ void RefactoringClient::addSearchResult(const ClangBackEnd::SourceRangeWithTextC searchHandle_->addResult(filePaths[sourceRangeWithText.fileHash()], sourceRangeWithText.text(), {{int(sourceRangeWithText.start().line()), - int(sourceRangeWithText.start().column())}, + int(sourceRangeWithText.start().column() - 1), + int(sourceRangeWithText.start().offset())}, {int(sourceRangeWithText.end().line()), - int(sourceRangeWithText.end().column())}}); + int(sourceRangeWithText.end().column() - 1), + int(sourceRangeWithText.end().offset())}}); } void RefactoringClient::setResultCounterAndSendSearchIsFinishedIfFinished() diff --git a/src/plugins/coreplugin/find/searchresultitem.h b/src/plugins/coreplugin/find/searchresultitem.h index 419cd8db078..dd4788687d2 100644 --- a/src/plugins/coreplugin/find/searchresultitem.h +++ b/src/plugins/coreplugin/find/searchresultitem.h @@ -25,6 +25,8 @@ #pragma once +#include + #include #include #include @@ -38,9 +40,11 @@ class TextPosition public: TextPosition() = default; TextPosition(int line, int column) : line(line), column(column) {} + TextPosition(int line, int column, int offset) : line(line), column(column), offset(offset) {} int line = -1; // (0 or -1 for no line number) int column = -1; // 0-based starting position for a mark (-1 for no mark) + int offset = -1; }; class TextRange @@ -57,12 +61,20 @@ public: return QString(); } + int endLineOffsetDifference() const + { + if (Utils::HostOsInfo::isWindowsHost()) + return begin.line - end.line; + + return 0; + } + int length() const { if (begin.line == end.line) return end.column - begin.column; - return 0; + return end.offset - begin.offset - endLineOffsetDifference(); } TextPosition begin; diff --git a/tests/unit/unittest/compare-operators.h b/tests/unit/unittest/compare-operators.h new file mode 100644 index 00000000000..54f397165b6 --- /dev/null +++ b/tests/unit/unittest/compare-operators.h @@ -0,0 +1,48 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#pragma once + +#include + +namespace Core { +namespace Search { + +inline +bool operator==(const TextPosition first, class TextPosition second) +{ + return first.line == second.line + && first.column == second.column + && first.offset == second.offset; +} + +inline +bool operator==(const TextRange first, class TextRange second) +{ + return first.begin == second.begin + && first.end == second.end; +} +} +} diff --git a/tests/unit/unittest/googletest.h b/tests/unit/unittest/googletest.h index a3a74ded2f9..ac488651b67 100644 --- a/tests/unit/unittest/googletest.h +++ b/tests/unit/unittest/googletest.h @@ -29,7 +29,10 @@ #include #include +#include "compare-operators.h" + #include "gtest-qt-printing.h" +#include "gtest-creator-printing.h" #ifdef CLANG_UNIT_TESTS # include "gtest-clang-printing.h" #endif diff --git a/tests/unit/unittest/gtest-creator-printing.cpp b/tests/unit/unittest/gtest-creator-printing.cpp new file mode 100644 index 00000000000..5980ec6c63e --- /dev/null +++ b/tests/unit/unittest/gtest-creator-printing.cpp @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#include "gtest-creator-printing.h" + +#include + +#include + +namespace Core { +namespace Search { + +using testing::PrintToString; + +class TextPosition; +class TextRange; + +void PrintTo(const TextPosition &position, ::std::ostream *os) +{ + *os << "(" + << position.line << ", " + << position.column << ", " + << position.offset << ")"; +} + +void PrintTo(const TextRange &range, ::std::ostream *os) +{ + *os << "(" + << PrintToString(range.begin) << ", " + << PrintToString(range.end) << ")"; +} + +} +} diff --git a/tests/unit/unittest/gtest-creator-printing.h b/tests/unit/unittest/gtest-creator-printing.h new file mode 100644 index 00000000000..7792475d8fb --- /dev/null +++ b/tests/unit/unittest/gtest-creator-printing.h @@ -0,0 +1,42 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#pragma once + +#include + +#include + +namespace Core { +namespace Search { + +class TextPosition; +class TextRange; + +void PrintTo(const TextPosition &position, ::std::ostream *os); +void PrintTo(const TextRange &range, ::std::ostream *os); + +} +} diff --git a/tests/unit/unittest/refactoringclient-test.cpp b/tests/unit/unittest/refactoringclient-test.cpp index 5f779223c92..03887a9b236 100644 --- a/tests/unit/unittest/refactoringclient-test.cpp +++ b/tests/unit/unittest/refactoringclient-test.cpp @@ -232,6 +232,18 @@ TEST_F(RefactoringClient, ConvertFilePaths) ASSERT_THAT(qstringFilePaths, Contains(Pair(42u, qStringFilePath))); } +TEST_F(RefactoringClient, XXX) +{ + const Core::Search::TextRange textRange{{1,0,1},{1,0,1}}; + const ClangBackEnd::SourceRangeWithTextContainer sourceRange{1, 1, 1, 1, 1, 1, 1, "function"}; + std::unordered_map filePaths = {{1, "/path/to/file"}}; + + EXPECT_CALL(mockSearchHandle, addResult(QString("/path/to/file"), QString("function"), textRange)) + .Times(1); + + client.addSearchResult(sourceRange, filePaths); +} + void RefactoringClient::SetUp() { client.setRefactoringEngine(&engine); diff --git a/tests/unit/unittest/unittest.pro b/tests/unit/unittest/unittest.pro index 601efbbd911..24673668a01 100644 --- a/tests/unit/unittest/unittest.pro +++ b/tests/unit/unittest/unittest.pro @@ -35,7 +35,8 @@ SOURCES += \ spydummy.cpp \ unittests-main.cpp \ utf8-test.cpp \ - gtest-qt-printing.cpp + gtest-qt-printing.cpp \ + gtest-creator-printing.cpp !isEmpty(LIBCLANG_LIBS) { SOURCES += \ @@ -123,6 +124,8 @@ HEADERS += \ mocksearchresult.h \ mocksearch.h \ mocksearchhandle.h \ + compare-operators.h \ + gtest-creator-printing.h !isEmpty(LIBCLANG_LIBS) { HEADERS += \