Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Tobias Hunger
qt-creator
Commits
fe1411cd
Commit
fe1411cd
authored
Oct 06, 2009
by
Roberto Raggi
Browse files
Use the existing text editor when refactoring text.
parent
debe1808
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/plugins/cpptools/cppfindreferences.cpp
View file @
fe1411cd
...
...
@@ -36,6 +36,7 @@
#include
<extensionsystem/pluginmanager.h>
#include
<utils/filesearch.h>
#include
<coreplugin/progressmanager/progressmanager.h>
#include
<coreplugin/editormanager/editormanager.h>
#include
<coreplugin/icore.h>
#include
<ASTVisitor.h>
...
...
@@ -574,6 +575,23 @@ void CppFindReferences::findAll_helper(Symbol *symbol)
connect
(
progress
,
SIGNAL
(
clicked
()),
_resultWindow
,
SLOT
(
popup
()));
}
static
void
applyChanges
(
QTextDocument
*
doc
,
const
QString
&
text
,
const
QList
<
Find
::
SearchResultItem
>
&
items
)
{
QList
<
QTextCursor
>
cursors
;
foreach
(
const
Find
::
SearchResultItem
&
item
,
items
)
{
const
int
blockNumber
=
item
.
lineNumber
-
1
;
QTextCursor
tc
(
doc
->
findBlockByNumber
(
blockNumber
));
tc
.
setPosition
(
tc
.
position
()
+
item
.
searchTermStart
);
tc
.
setPosition
(
tc
.
position
()
+
item
.
searchTermLength
,
QTextCursor
::
KeepAnchor
);
cursors
.
append
(
tc
);
}
foreach
(
QTextCursor
tc
,
cursors
)
tc
.
insertText
(
text
);
}
void
CppFindReferences
::
onReplaceButtonClicked
(
const
QString
&
text
,
const
QList
<
Find
::
SearchResultItem
>
&
items
)
{
...
...
@@ -585,41 +603,48 @@ void CppFindReferences::onReplaceButtonClicked(const QString &text,
foreach
(
const
Find
::
SearchResultItem
&
item
,
items
)
changes
[
item
.
fileName
].
append
(
item
);
Core
::
EditorManager
*
editorManager
=
Core
::
EditorManager
::
instance
();
QHashIterator
<
QString
,
QList
<
Find
::
SearchResultItem
>
>
it
(
changes
);
while
(
it
.
hasNext
())
{
it
.
next
();
const
QString
fileName
=
it
.
key
();
QFile
file
(
fileName
);
if
(
file
.
open
(
QFile
::
ReadOnly
))
{
QTextStream
stream
(
&
file
);
// ### set the encoding
const
QString
plainText
=
stream
.
readAll
();
file
.
close
();
QTextDocument
doc
;
doc
.
setPlainText
(
plainText
);
QList
<
QTextCursor
>
cursors
;
const
QList
<
Find
::
SearchResultItem
>
items
=
it
.
value
();
foreach
(
const
Find
::
SearchResultItem
&
item
,
items
)
{
const
int
blockNumber
=
item
.
lineNumber
-
1
;
QTextCursor
tc
(
doc
.
findBlockByNumber
(
blockNumber
));
tc
.
setPosition
(
tc
.
position
()
+
item
.
searchTermStart
);
tc
.
setPosition
(
tc
.
position
()
+
item
.
searchTermLength
,
QTextCursor
::
KeepAnchor
);
cursors
.
append
(
tc
);
}
const
QList
<
Find
::
SearchResultItem
>
items
=
it
.
value
();
foreach
(
QTextCursor
tc
,
cursors
)
tc
.
insertText
(
text
);
const
QList
<
Core
::
IEditor
*>
editors
=
editorManager
->
editorsForFileName
(
fileName
);
TextEditor
::
BaseTextEditor
*
textEditor
=
0
;
foreach
(
Core
::
IEditor
*
editor
,
editors
)
{
textEditor
=
qobject_cast
<
TextEditor
::
BaseTextEditor
*>
(
editor
->
widget
());
if
(
textEditor
!=
0
)
break
;
}
if
(
textEditor
!=
0
)
{
QTextCursor
tc
=
textEditor
->
textCursor
();
tc
.
beginEditBlock
();
applyChanges
(
textEditor
->
document
(),
text
,
items
);
tc
.
endEditBlock
();
}
else
{
QFile
file
(
fileName
);
QFile
newFile
(
fileName
);
if
(
newFile
.
open
(
QFile
::
WriteOnly
))
{
QTextStream
stream
(
&
newFile
);
if
(
file
.
open
(
QFile
::
ReadOnly
))
{
QTextStream
stream
(
&
file
);
// ### set the encoding
stream
<<
doc
.
toPlainText
();
const
QString
plainText
=
stream
.
readAll
();
file
.
close
();
QTextDocument
doc
;
doc
.
setPlainText
(
plainText
);
applyChanges
(
&
doc
,
text
,
items
);
QFile
newFile
(
fileName
);
if
(
newFile
.
open
(
QFile
::
WriteOnly
))
{
QTextStream
stream
(
&
newFile
);
// ### set the encoding
stream
<<
doc
.
toPlainText
();
}
}
}
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment