Clang: use HighlightingMarks for hover with Ctrl highlighting

Make this highlighting work without builtin code model
but based on the HighlightingMarks that we already have
from ClangCodeModel.
Redundant parameters are removed by this change.

Change-Id: I73b5dab46ba59d2f813236831818f0a9bc94c5bc
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
Ivan Donchevskii
2017-09-08 09:33:35 +02:00
parent c73793414b
commit f130de2400
31 changed files with 265 additions and 191 deletions

View File

@@ -23,14 +23,78 @@
**
****************************************************************************/
#include "clangfollowsymbol.h"
#include "clangeditordocumentprocessor.h"
#include "texteditor/texteditor.h"
#include "texteditor/convenience.h"
#include "clangfollowsymbol.h"
#include <texteditor/convenience.h>
#include <texteditor/texteditor.h>
#include <clangsupport/highlightingmarkcontainer.h>
#include <utils/algorithm.h>
namespace ClangCodeModel {
namespace Internal {
// Returns invalid Mark if it is not found at (line, column)
static bool findMark(const QVector<ClangBackEnd::HighlightingMarkContainer> &marks,
uint line,
uint column,
ClangBackEnd::HighlightingMarkContainer &mark)
{
mark = Utils::findOrDefault(marks,
[line, column](const ClangBackEnd::HighlightingMarkContainer &curMark) {
if (curMark.line() != line)
return false;
if (curMark.column() == column)
return true;
if (curMark.column() < column && curMark.column() + curMark.length() >= column)
return true;
return false;
});
if (mark.isInvalid())
return false;
return true;
}
static int getMarkPos(QTextCursor cursor, const ClangBackEnd::HighlightingMarkContainer &mark)
{
cursor.setPosition(0);
cursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, mark.line() - 1);
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, mark.column() - 1);
return cursor.position();
}
static TextEditor::TextEditorWidget::Link linkAtCursor(QTextCursor cursor,
const QString &filePath,
uint line,
uint column,
ClangEditorDocumentProcessor *processor)
{
using Link = TextEditor::TextEditorWidget::Link;
const QVector<ClangBackEnd::HighlightingMarkContainer> &marks
= processor->highlightingMarks();
ClangBackEnd::HighlightingMarkContainer mark;
if (!findMark(marks, line, column, mark))
return Link();
cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
const QString tokenStr = cursor.selectedText();
Link token(filePath, mark.line(), mark.column());
token.linkTextStart = getMarkPos(cursor, mark);
token.linkTextEnd = token.linkTextStart + mark.length();
if (mark.isIncludeDirectivePath()) {
if (tokenStr != "include" && tokenStr != "#" && tokenStr != "<")
return token;
return Link();
}
if (mark.isIdentifier() || tokenStr == "operator")
return token;
return Link();
}
TextEditor::TextEditorWidget::Link ClangFollowSymbol::findLink(
const CppTools::CursorInEditor &data,
bool resolveTarget,
@@ -39,42 +103,39 @@ TextEditor::TextEditorWidget::Link ClangFollowSymbol::findLink(
CppTools::SymbolFinder *,
bool)
{
Link link;
int lineNumber = 0, positionInBlock = 0;
QTextCursor cursor = TextEditor::Convenience::wordStartCursor(data.cursor());
TextEditor::Convenience::convertPosition(cursor.document(), cursor.position(), &lineNumber,
&positionInBlock);
const unsigned line = lineNumber;
const unsigned column = positionInBlock + 1;
if (!resolveTarget)
return link;
const uint line = lineNumber;
const uint column = positionInBlock + 1;
ClangEditorDocumentProcessor *processor = ClangEditorDocumentProcessor::get(
data.filePath().toString());
if (!processor)
return link;
return Link();
if (!resolveTarget)
return linkAtCursor(cursor, data.filePath().toString(), line, column, processor);
QFuture<CppTools::SymbolInfo> info
= processor->requestFollowSymbol(static_cast<int>(line),
static_cast<int>(column),
resolveTarget);
static_cast<int>(column));
if (info.isCanceled())
return link;
return Link();
while (!info.isFinished()) {
if (info.isCanceled())
return link;
return Link();
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
}
CppTools::SymbolInfo result = info.result();
if (result.failedToFollow)
return link;
// We did not fail but the result is empty
if (result.fileName.isEmpty())
return link;
return Link();
return Link(result.fileName, result.startLine, result.startColumn - 1);
}