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
040ce975
Commit
040ce975
authored
15 years ago
by
Roberto Raggi
Browse files
Options
Downloads
Patches
Plain Diff
Split or conditions.
parent
a3580db7
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
+68
-3
68 additions, 3 deletions
src/plugins/cppeditor/cppquickfix.cpp
with
68 additions
and
3 deletions
src/plugins/cppeditor/cppquickfix.cpp
+
68
−
3
View file @
040ce975
...
@@ -152,6 +152,16 @@ private:
...
@@ -152,6 +152,16 @@ private:
if (something_else) {
if (something_else) {
}
}
}
}
and
if (something || something_else)
x;
with
if (something)
x;
else if (something_else)
x;
*/
*/
class
SplitIfStatementOp
:
public
QuickFixOperation
class
SplitIfStatementOp
:
public
QuickFixOperation
{
{
...
@@ -174,7 +184,8 @@ public:
...
@@ -174,7 +184,8 @@ public:
if
(
statement
->
match
(
pattern
,
&
matcher
)
if
(
statement
->
match
(
pattern
,
&
matcher
)
&&
pattern
->
statement
&&
pattern
->
statement
&&
pattern
->
rparen_token
&&
pattern
->
rparen_token
&&
tokenAt
(
condition
->
binary_op_token
).
is
(
T_AMPER_AMPER
))
&&
(
tokenAt
(
condition
->
binary_op_token
).
is
(
T_AMPER_AMPER
)
||
tokenAt
(
condition
->
binary_op_token
).
is
(
T_PIPE_PIPE
)))
return
true
;
return
true
;
return
false
;
return
false
;
...
@@ -182,9 +193,16 @@ public:
...
@@ -182,9 +193,16 @@ public:
virtual
void
apply
()
virtual
void
apply
()
{
{
StatementAST
*
ifTrueStatement
=
pattern
->
statement
;
const
Token
binaryOp
=
tokenAt
(
condition
->
binary_op_token
);
CompoundStatementAST
*
compoundStatement
=
ifTrueStatement
->
asCompoundStatement
();
if
(
binaryOp
.
is
(
T_AMPER_AMPER
))
splitAndCondition
();
else
splitOrCondition
();
}
void
splitAndCondition
()
{
QTextCursor
completeIfStatement
=
selectNode
(
pattern
);
QTextCursor
completeIfStatement
=
selectNode
(
pattern
);
{
{
// ### HACK
// ### HACK
...
@@ -194,6 +212,9 @@ public:
...
@@ -194,6 +212,9 @@ public:
completeIfStatement
.
setPosition
(
anchor
,
QTextCursor
::
KeepAnchor
);
completeIfStatement
.
setPosition
(
anchor
,
QTextCursor
::
KeepAnchor
);
}
}
StatementAST
*
ifTrueStatement
=
pattern
->
statement
;
CompoundStatementAST
*
compoundStatement
=
ifTrueStatement
->
asCompoundStatement
();
// take the right-expression from the condition.
// take the right-expression from the condition.
const
QString
rightCondition
=
selectNode
(
condition
->
right_expression
).
selectedText
();
const
QString
rightCondition
=
selectNode
(
condition
->
right_expression
).
selectedText
();
replace
(
endOf
(
condition
->
left_expression
),
startOf
(
pattern
->
rparen_token
),
QString
());
replace
(
endOf
(
condition
->
left_expression
),
startOf
(
pattern
->
rparen_token
),
QString
());
...
@@ -227,6 +248,50 @@ public:
...
@@ -227,6 +248,50 @@ public:
tc
.
endEditBlock
();
tc
.
endEditBlock
();
}
}
void
splitOrCondition
()
{
QTextCursor
completeIfStatement
=
selectNode
(
pattern
);
{
// ### HACK
const
int
anchor
=
completeIfStatement
.
anchor
();
const
int
position
=
completeIfStatement
.
position
();
completeIfStatement
.
setPosition
(
position
);
completeIfStatement
.
setPosition
(
anchor
,
QTextCursor
::
KeepAnchor
);
}
StatementAST
*
ifTrueStatement
=
pattern
->
statement
;
CompoundStatementAST
*
compoundStatement
=
ifTrueStatement
->
asCompoundStatement
();
// 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
());
// copy the if-body
QTextCursor
bodyCursor
=
textCursor
();
bodyCursor
.
setPosition
(
endOf
(
pattern
->
rparen_token
));
bodyCursor
.
setPosition
(
endOf
(
pattern
->
statement
),
QTextCursor
::
KeepAnchor
);
const
QString
body
=
bodyCursor
.
selectedText
();
QString
elseIfStatement
;
if
(
compoundStatement
)
elseIfStatement
+=
QLatin1String
(
" "
);
else
elseIfStatement
+=
QLatin1String
(
"
\n
"
);
elseIfStatement
+=
QLatin1String
(
"else if ("
);
elseIfStatement
+=
rightCondition
;
elseIfStatement
+=
QLatin1String
(
")"
);
elseIfStatement
+=
body
;
insert
(
endOf
(
pattern
),
elseIfStatement
);
QTextCursor
tc
=
textCursor
();
tc
.
beginEditBlock
();
execute
();
editor
->
indentInsertedText
(
completeIfStatement
);
tc
.
endEditBlock
();
}
private
:
private
:
ASTMatcher
matcher
;
ASTMatcher
matcher
;
ASTPatternBuilder
mk
;
ASTPatternBuilder
mk
;
...
...
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