Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Q
qt-creator
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Tobias Hunger
qt-creator
Commits
a3580db7
Commit
a3580db7
authored
15 years ago
by
Roberto Raggi
Browse files
Options
Downloads
Patches
Plain Diff
Split if statements.
parent
f3f5ce5e
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/plugins/cppeditor/cppquickfix.cpp
+104
-5
104 additions, 5 deletions
src/plugins/cppeditor/cppquickfix.cpp
with
104 additions
and
5 deletions
src/plugins/cppeditor/cppquickfix.cpp
+
104
−
5
View file @
a3580db7
...
...
@@ -142,6 +142,98 @@ private:
BinaryExpressionAST
*
pattern
;
};
/*
Replace
if (something && something_else) {
}
with
if (something) {
if (something_else) {
}
}
*/
class
SplitIfStatementOp
:
public
QuickFixOperation
{
public:
SplitIfStatementOp
(
Document
::
Ptr
doc
,
const
Snapshot
&
snapshot
,
CPPEditor
*
editor
)
:
QuickFixOperation
(
doc
,
snapshot
),
matcher
(
doc
->
translationUnit
()),
condition
(
0
),
pattern
(
0
),
editor
(
editor
)
{}
virtual
QString
description
()
const
{
return
QLatin1String
(
"Split if statement"
);
// ### tr?
}
bool
match
(
IfStatementAST
*
statement
)
{
condition
=
mk
.
BinaryExpression
();
pattern
=
mk
.
IfStatement
(
condition
);
if
(
statement
->
match
(
pattern
,
&
matcher
)
&&
pattern
->
statement
&&
pattern
->
rparen_token
&&
tokenAt
(
condition
->
binary_op_token
).
is
(
T_AMPER_AMPER
))
return
true
;
return
false
;
}
virtual
void
apply
()
{
StatementAST
*
ifTrueStatement
=
pattern
->
statement
;
CompoundStatementAST
*
compoundStatement
=
ifTrueStatement
->
asCompoundStatement
();
QTextCursor
completeIfStatement
=
selectNode
(
pattern
);
{
// ### HACK
const
int
anchor
=
completeIfStatement
.
anchor
();
const
int
position
=
completeIfStatement
.
position
();
completeIfStatement
.
setPosition
(
position
);
completeIfStatement
.
setPosition
(
anchor
,
QTextCursor
::
KeepAnchor
);
}
// take the right-expression from the condition.
const
QString
rightCondition
=
selectNode
(
condition
->
right_expression
).
selectedText
();
replace
(
endOf
(
condition
->
left_expression
),
startOf
(
pattern
->
rparen_token
),
QString
());
int
offset
=
0
;
if
(
compoundStatement
)
offset
=
endOf
(
compoundStatement
->
lbrace_token
);
else
offset
=
endOf
(
pattern
->
rparen_token
);
// create the nested if statement
QString
nestedIfStatement
;
if
(
!
compoundStatement
)
nestedIfStatement
+=
QLatin1String
(
" {"
);
// open a compound statement
nestedIfStatement
+=
QLatin1String
(
"
\n
if ("
);
nestedIfStatement
+=
rightCondition
;
nestedIfStatement
+=
QLatin1String
(
") {
\n
}"
);
insert
(
offset
,
nestedIfStatement
);
if
(
!
compoundStatement
)
insert
(
endOf
(
ifTrueStatement
),
"
\n
}"
);
// finish the compound statement
QTextCursor
tc
=
textCursor
();
tc
.
beginEditBlock
();
execute
();
editor
->
indentInsertedText
(
completeIfStatement
);
tc
.
endEditBlock
();
}
private
:
ASTMatcher
matcher
;
ASTPatternBuilder
mk
;
BinaryExpressionAST
*
condition
;
IfStatementAST
*
pattern
;
QPointer
<
CPPEditor
>
editor
;
};
}
// end of anonymous namespace
...
...
@@ -279,15 +371,22 @@ int CPPQuickFixCollector::startCompletion(TextEditor::ITextEditable *editable)
const
QList
<
AST
*>
path
=
astPath
(
_editor
->
textCursor
());
// ### build the list of the quick fix ops by scanning path.
RewriteLogicalAndOp
*
op
=
new
RewriteLogicalAndOp
(
info
.
doc
,
info
.
snapshot
);
Q
uickFixOperationPtr
quickFix
(
op
);
QSharedPointer
<
RewriteLogicalAndOp
>
rewriteLogicalAndOp
(
new
RewriteLogicalAndOp
(
info
.
doc
,
info
.
snapshot
)
)
;
Q
SharedPointer
<
SplitIfStatementOp
>
splitIfStatement
(
new
SplitIfStatementOp
(
info
.
doc
,
info
.
snapshot
,
_editor
)
);
for
(
int
i
=
path
.
size
()
-
1
;
i
!=
-
1
;
--
i
)
{
AST
*
node
=
path
.
at
(
i
);
// ### TODO: generalize
if
(
BinaryExpressionAST
*
binary
=
node
->
asBinaryExpression
())
{
if
(
op
->
match
(
binary
))
{
_quickFixes
.
append
(
quickFix
);
break
;
if
(
!
_quickFixes
.
contains
(
rewriteLogicalAndOp
)
&&
rewriteLogicalAndOp
->
match
(
binary
))
{
_quickFixes
.
append
(
rewriteLogicalAndOp
);
}
}
else
if
(
IfStatementAST
*
ifStatement
=
node
->
asIfStatement
())
{
if
(
!
_quickFixes
.
contains
(
splitIfStatement
)
&&
splitIfStatement
->
match
(
ifStatement
))
{
_quickFixes
.
append
(
splitIfStatement
);
}
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment