Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Intellij Community
Commits
8c625ecb
Commit
8c625ecb
authored
9 years ago
by
Anton Makeev
Browse files
Options
Download
Email Patches
Plain Diff
OCEnterAfterUnmatchedBraceHandler.java 0% duplication, no strange API
parent
f1049564
Branches unavailable
Tags unavailable
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
platform/lang-impl/src/com/intellij/codeInsight/editorActions/enter/EnterAfterUnmatchedBraceHandler.java
+39
-41
.../editorActions/enter/EnterAfterUnmatchedBraceHandler.java
with
39 additions
and
41 deletions
+39
-41
platform/lang-impl/src/com/intellij/codeInsight/editorActions/enter/EnterAfterUnmatchedBraceHandler.java
+
39
-
41
View file @
8c625ecb
...
...
@@ -57,30 +57,16 @@ public class EnterAfterUnmatchedBraceHandler extends EnterHandlerDelegateAdapter
final
EditorActionHandler
originalHandler
)
{
int
caretOffset
=
caretOffsetRef
.
get
();
int
maxRBraceCount
=
getMaxRBraceCount
(
file
,
editor
,
caretOffset
);
if
(
maxRBraceCount
>
0
&&
insertRBraces
(
file
,
editor
,
caretOffset
,
getRBraceOffset
(
file
,
editor
,
caretOffset
),
adjustRBraceCountForPosition
(
editor
,
caretOffset
,
maxRBraceCount
)))
{
return
Result
.
DefaultForceIndent
;
}
return
Result
.
Continue
;
}
if
(!
CodeInsightSettings
.
getInstance
().
INSERT_BRACE_ON_ENTER
)
return
Result
.
Continue
;
/**
* Calculates the maximum number of '}' that can be inserted by handler.
* Can return <code>0</code> or less in custom implementation to skip '}' insertion in the <code>preprocessEnter</code> call
* and switch to default implementation.
*
* @param file target PSI file
* @param editor target editor
* @param caretOffset target caret offset
* @return maximum number of '}' that can be inserted by handler, <code>0</code> or less to switch to default implementation
*/
protected
int
getMaxRBraceCount
(
@NotNull
final
PsiFile
file
,
@NotNull
final
Editor
editor
,
int
caretOffset
)
{
if
(!
CodeInsightSettings
.
getInstance
().
INSERT_BRACE_ON_ENTER
)
{
return
0
;
}
return
Math
.
max
(
0
,
getUnmatchedLBracesNumberBefore
(
editor
,
caretOffset
,
file
.
getFileType
()));
int
maxRBraceCount
=
getUnmatchedLBracesNumberBefore
(
editor
,
caretOffset
,
file
.
getFileType
());
if
(
maxRBraceCount
<=
0
)
return
Result
.
Continue
;
insertRBraces
(
file
,
editor
,
caretOffset
,
getRBraceOffset
(
file
,
editor
,
caretOffset
),
adjustRBraceCountForPosition
(
editor
,
caretOffset
,
maxRBraceCount
));
return
Result
.
DefaultForceIndent
;
}
/**
...
...
@@ -96,23 +82,28 @@ public class EnterAfterUnmatchedBraceHandler extends EnterHandlerDelegateAdapter
CharSequence
text
=
editor
.
getDocument
().
getCharsSequence
();
int
bracesToInsert
=
0
;
outer:
for
(
int
i
=
caretOffset
-
1
;
i
>=
0
&&
bracesToInsert
<
maxRBraceCount
;
--
i
)
{
switch
(
text
.
charAt
(
i
))
{
case
' '
:
case
'\n'
:
case
'\t'
:
continue
;
case
'{'
:
bracesToInsert
++;
break
;
default
:
break
outer
;
char
c
=
text
.
charAt
(
i
);
if
(
c
==
'{'
)
{
bracesToInsert
++;
}
else
if
(
breakOn
(
c
))
{
break
;
}
}
return
Math
.
max
(
bracesToInsert
,
1
);
}
protected
boolean
breakOn
(
char
ch
)
{
switch
(
ch
)
{
case
' '
:
case
'\n'
:
case
'\t'
:
return
false
;
}
return
true
;
}
/**
* Calculates the position for insertion of one or more '}'.
*
...
...
@@ -142,11 +133,11 @@ public class EnterAfterUnmatchedBraceHandler extends EnterHandlerDelegateAdapter
* @param rBracesCount count of '}' to insert
* @return true for success
*/
protected
boolean
insertRBraces
(
@NotNull
PsiFile
file
,
@NotNull
Editor
editor
,
int
caretOffset
,
int
rBracesInsertOffset
,
int
rBracesCount
)
{
protected
void
insertRBraces
(
@NotNull
PsiFile
file
,
@NotNull
Editor
editor
,
int
caretOffset
,
int
rBracesInsertOffset
,
int
rBracesCount
)
{
final
Document
document
=
editor
.
getDocument
();
document
.
insertString
(
rBracesInsertOffset
,
"\n"
+
StringUtil
.
repeatSymbol
(
'}'
,
rBracesCount
));
// We need to adjust indents of the text that will be moved, hence, need to insert preliminary line feed.
...
...
@@ -171,7 +162,7 @@ public class EnterAfterUnmatchedBraceHandler extends EnterHandlerDelegateAdapter
boolean
closingBraceIndentAdjusted
;
try
{
PsiDocumentManager
.
getInstance
(
project
).
commitDocument
(
document
);
CodeStyleManager
.
getInstance
(
project
).
adjustLineIndent
(
file
,
new
TextRange
(
caretOffset
,
rBracesInsertOffset
+
2
)
);
doAlignt
(
file
,
caretOffset
,
rBracesInsertOffset
,
rBracesCount
);
}
catch
(
IncorrectOperationException
e
)
{
LOG
.
error
(
e
);
...
...
@@ -202,7 +193,10 @@ public class EnterAfterUnmatchedBraceHandler extends EnterHandlerDelegateAdapter
document
.
insertString
(
rBracesInsertOffset
+
1
,
buffer
);
}
}
return
true
;
}
protected
void
doAlignt
(
@NotNull
PsiFile
file
,
int
caretOffset
,
int
rBracesInsertOffset
,
int
rBracesCount
)
{
CodeStyleManager
.
getInstance
(
file
.
getProject
()).
adjustLineIndent
(
file
,
new
TextRange
(
caretOffset
,
rBracesInsertOffset
+
2
));
}
/**
...
...
@@ -253,6 +247,10 @@ public class EnterAfterUnmatchedBraceHandler extends EnterHandlerDelegateAdapter
if
(
element
.
getTextOffset
()
!=
offset
)
{
return
CharArrayUtil
.
shiftForwardUntil
(
text
,
offset
,
"\n"
);
}
return
doCalcOffset
(
element
);
}
protected
int
doCalcOffset
(
PsiElement
element
)
{
return
element
.
getTextRange
().
getEndOffset
();
}
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a 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
Menu
Projects
Groups
Snippets
Help