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 <tim.jenssen@qt.io>
This commit is contained in:
Tim Jenssen
2016-11-23 13:31:47 +01:00
parent 4aae6b730f
commit 6fe90532dc
10 changed files with 184 additions and 6 deletions

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -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()

View File

@@ -25,6 +25,8 @@
#pragma once
#include <utils/hostosinfo.h>
#include <QIcon>
#include <QStringList>
#include <QVariant>
@@ -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;

View File

@@ -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 <coreplugin/find/searchresultitem.h>
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;
}
}
}

View File

@@ -29,7 +29,10 @@
#include <gmock/gmock-matchers.h>
#include <gtest/gtest.h>
#include "compare-operators.h"
#include "gtest-qt-printing.h"
#include "gtest-creator-printing.h"
#ifdef CLANG_UNIT_TESTS
# include "gtest-clang-printing.h"
#endif

View File

@@ -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 <gtest/gtest-printers.h>
#include <coreplugin/find/searchresultitem.h>
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) << ")";
}
}
}

View File

@@ -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 <QtGlobal>
#include <iosfwd>
namespace Core {
namespace Search {
class TextPosition;
class TextRange;
void PrintTo(const TextPosition &position, ::std::ostream *os);
void PrintTo(const TextRange &range, ::std::ostream *os);
}
}

View File

@@ -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<uint, QString> 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);

View File

@@ -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 += \