-
Notifications
You must be signed in to change notification settings - Fork 23
/
VlHoverHandler.cpp
157 lines (141 loc) · 5.42 KB
/
VlHoverHandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Copyright 2018 Rochus Keller <mailto:me@rochus-keller.ch>
*
* This file is part of the VerilogCreator plugin.
*
* The following is the license that applies to this copy of the
* plugin. For a license to use the plugin under conditions
* other than those described here, please email to me@rochus-keller.ch.
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include "VlHoverHandler.h"
#include "VlModelManager.h"
#include <Verilog/VlCrossRefModel.h>
#include <Verilog/VlPpSymbols.h>
#include <Verilog/VlIncludes.h>
#include <texteditor/texteditor.h>
#include <texteditor/textdocument.h>
#include <projectexplorer/projecttree.h>
#include <projectexplorer/project.h>
#include <utils/fileutils.h>
#include <QTextBlock>
#include <QTextStream>
#include <QTextCursor>
#include <QtDebug>
using namespace Vl;
VerilogHoverHandler::VerilogHoverHandler()
{
}
static inline bool isCond( Directive di )
{
return di == Cd_ifdef || di == Cd_ifndef || di == Cd_elsif || di == Cd_undef;
}
static inline QByteArray escape( QByteArray str )
{
return str.trimmed();
}
static QString prettyPrint( const PpSymbols::Define& d )
{
QString str;
QTextStream out( &str, QIODevice::WriteOnly );
out << "`define " << d.d_name;
if( !d.d_args.isEmpty() )
{
out << "( ";
out << d.d_args.join(", ");
out << " ) ";
}else
out << " ";
foreach( const Token& t, d.d_toks )
{
if( t.d_type < TT_Specials )
out << t.getName();
else if( t.d_type == Tok_Str )
out << "\"" << escape(t.d_val) << "\"";
else if( t.d_type == Tok_SysName )
out << "$" << t.d_val;
else
out << t.d_val;
out << " ";
}
return str;
}
void VerilogHoverHandler::identifyMatch(TextEditor::TextEditorWidget* editorWidget, int pos)
{
QString text = editorWidget->extraSelectionTooltip(pos);
if( !text.isEmpty() )
{
setToolTip(text);
return;
}
// else
QTextCursor cur(editorWidget->document());
cur.setPosition(pos);
const QString file = editorWidget->textDocument()->filePath().toString();
CrossRefModel* mdl = ModelManager::instance()->getModelForCurrentProjectOrDirPath(file);
const int line = cur.blockNumber() + 1;
const int col = cur.columnNumber() + 1;
int tokPos;
QList<Token> toks = CrossRefModel::findTokenByPos( cur.block().text(), col, &tokPos,
mdl->getFcache()->supportSvExt(file) );
if( tokPos != -1 )
{
const Token& t = toks[tokPos];
if( t.d_type == Tok_CoDi && matchDirective(t.d_val) == Cd_Invalid )
{
PpSymbols::Define d = mdl->getSyms()->getSymbol( t.d_val );
if( !d.d_name.isEmpty() )
setToolTip( prettyPrint(d) );
}else if( t.d_type == Tok_Str && tokPos > 0 && toks[tokPos-1].d_type == Tok_CoDi &&
matchDirective(toks[tokPos-1].d_val) == Cd_include )
{
const QString path = mdl->getIncs()->findPath( t.d_val, file );
if( !path.isEmpty() )
setToolTip( path );
}else if( t.d_type == Tok_Ident && tokPos > 0 && toks[tokPos-1].d_type == Tok_CoDi &&
isCond( matchDirective( toks[tokPos-1].d_val) ) )
{
PpSymbols::Define d = mdl->getSyms()->getSymbol( t.d_val );
if( !d.d_name.isEmpty() )
setToolTip( prettyPrint(d) );
}else if( t.d_type == Tok_Ident )
{
CrossRefModel::TreePath path = mdl->findSymbolBySourcePos( file, line, col );
if( path.isEmpty() )
return;
CrossRefModel::IdentDeclRef decl = mdl->findDeclarationOfSymbol(path.first().data());
if( decl.data() == 0 )
return;
// qDebug() << "declared" << decl->decl()->tok().d_sourcePath << decl->decl()->tok().d_lineNr;
FileCache* fcache = ModelManager::instance()->getFileCache();
const QByteArray line = fcache->fetchTextLineFromFile(
decl->decl()->tok().d_sourcePath, decl->decl()->tok().d_lineNr );
QTextStream out(&text);
QStringList parts = CrossRefModel::qualifiedNameParts(path,true);
for( int l = 0; l < parts.size(); l++ )
{
if( l != 0 )
out << endl << QString(l*3,QChar(' ')) << QChar('.');
out << parts[l];
}
int len = decl->decl()->tok().d_len;
if( len == 0 )
len = -1;
// TODO: für Ports in alter Delkaration nicht optimal
out << endl << QString::fromLatin1(line.mid(decl->decl()->tok().d_colNr-1,len).simplified())
<< " " << QString::fromLatin1(decl->tok().d_val);
if( decl->decl()->tok().d_sourcePath != file )
out << endl << tr("declared in ") << QFileInfo(decl->decl()->tok().d_sourcePath).fileName();
setToolTip( text );
// NOTE: mit html funktioniert es nicht!
}
}
}