-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
Copy pathBlock.java
141 lines (129 loc) · 5.23 KB
/
Block.java
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
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.formatting;
import com.intellij.openapi.util.TextRange;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
/**
* Describes a single block in the {@link FormattingModel}.
*
* @see FormattingModel#getRootBlock()
*/
public interface Block {
/**
* Returns the text range covered by the block.
*
* @return the text range.
*/
@NotNull
TextRange getTextRange();
/**
* Returns the list of child blocks for the specified block. <b>Important</b>: The same list
* of blocks must be returned when {@code getSubBlocks()} is repeatedly called on a particular
* {@code Block} instance.
* <b>Even more Important</b>:
* If immutable list is returned (e.g. {@code Collections.singletonList(xxx)}, you must mark this {@link Block} class with {@link com.intellij.formatting.ReadOnlyBlockContainer} marker interface.
*
* @return the child block list.
* @see #isLeaf()
*/
@NotNull
List<Block> getSubBlocks();
/**
* Returns a wrap object indicating the conditions under which a line break
* is inserted before this block when formatting, if the block extends beyond the
* right margin.
*
* @return the wrap object, or null if the line break is never inserted.
* @see Wrap#createWrap(WrapType, boolean)
* @see Wrap#createChildWrap(Wrap, WrapType, boolean)
*/
@Nullable
Wrap getWrap();
/**
* Returns an indent object indicating how this block is indented relative
* to its parent block.
*
* @return the indent object, or null if the default indent ("continuation without first") should be used.
* @see Indent#getContinuationWithoutFirstIndent()
*/
@Nullable
Indent getIndent();
/**
* Returns an alignment object indicating how this block is aligned with other blocks. Blocks
* which return the same alignment object instance from the {@code getAlignment} method
* are aligned with each other.
*
* @return the alignment object instance, or null if no alignment is required for the block.
*/
@Nullable
Alignment getAlignment();
/**
* Returns a spacing object indicating what spaces and/or line breaks are added between two
* specified children of this block.
*
* @param child1 the first child for which spacing is requested;
* {@code null} if given {@code 'child2'} block is the first document block
* @param child2 the second child for which spacing is requested.
* @return the spacing instance, or null if no special spacing is required. If null is returned,
* the formatter does not insert or delete spaces between the child blocks, but may insert
* a line break if the line wraps at the position between the child blocks.
* @see Spacing#createSpacing(int, int, int, boolean, int)
* @see Spacing#getReadOnlySpacing()
*/
@Nullable
Spacing getSpacing(@Nullable Block child1, @NotNull Block child2);
/**
* Returns the alignment and indent attributes which are applied to a new block inserted at
* the specified position in the list of children of this block. Used for performing automatic
* indent when Enter is pressed.
*
* @param newChildIndex the index where a new child is inserted.
* @return the object containing the indent and alignment settings for the new child.
*/
@NotNull
ChildAttributes getChildAttributes(final int newChildIndex);
/**
* Checks if the current block is incomplete (contains elements that the user will
* probably type but has not yet typed). For example, a parameter list is incomplete if
* it does not have the trailing parenthesis, and a statement is incomplete if it does not
* have the trailing semicolon. Used to determine the block for which {@link #getChildAttributes(int)}
* is called when Enter is pressed: if the block immediately before the cursor is incomplete,
* the method is called for that block; otherwise, the method is called for the parent of that block.
*
* @return true if the block is incomplete, false otherwise.
*/
boolean isIncomplete();
/**
* Returns true if the specified block may not contain child blocks. Used as an optimization
* to avoid building the complete formatting model through calls to {@link #getSubBlocks()}.
*
* @return true if the block is a leaf block and may not contain child blocks, false otherwise.
*/
boolean isLeaf();
/**
* Returns an internal debug name, used in the Block Structure of PSI Viewer.
*
* By default it returns null, in this case the PSI Viewer uses the simple class name.
*
* @return debug name, or null for default one.
*/
@Nullable
default String getDebugName() {
return null;
}
}