|
1 | 1 | #include "CodeService/FormatElement/FormatElement.h" |
| 2 | +#include "Util/format.h" |
2 | 3 |
|
3 | 4 | FormatElement::FormatElement(TextRange range) |
4 | 5 | : _textRange(range) |
@@ -132,6 +133,72 @@ int FormatElement::GetNextValidLine(FormatContext& ctx, ChildIterator it, Format |
132 | 133 | } |
133 | 134 | } |
134 | 135 |
|
| 136 | +void FormatElement::GeneralIndentDiagnosis(DiagnosisContext& ctx, ChildIterator selfIt, FormatElement& parent) |
| 137 | +{ |
| 138 | + for (auto it = _children.begin(); it != _children.end(); ++it) |
| 139 | + { |
| 140 | + const auto child = *it; |
| 141 | + |
| 142 | + if (child->HasValidTextRange() |
| 143 | + && child->GetType() != FormatElementType::IndentElement |
| 144 | + && child->GetType() != FormatElementType::NoIndentElement) |
| 145 | + { |
| 146 | + auto writeCharacter = ctx.GetCharacterCount(); |
| 147 | + if (writeCharacter != 0) |
| 148 | + { |
| 149 | + goto endIndentDiagnose; |
| 150 | + } |
| 151 | + |
| 152 | + auto range = child->GetTextRange(); |
| 153 | + auto line = ctx.GetLine(range.StartOffset); |
| 154 | + auto character = ctx.GetColumn(range.StartOffset); |
| 155 | + auto indentState = ctx.CalculateIndentState(range.StartOffset); |
| 156 | + auto state = ctx.GetCurrentIndent(); |
| 157 | + if (ctx.GetOptions().indent_style != indentState.Style) |
| 158 | + { |
| 159 | + ctx.PushDiagnosis( |
| 160 | + format(LText("incorrect indentation style, expect {}, but here is {}"), |
| 161 | + GetIndentStyleName(state.Style), |
| 162 | + GetIndentStyleName(indentState.Style) |
| 163 | + ), |
| 164 | + LuaDiagnosisPosition(line, 0), |
| 165 | + LuaDiagnosisPosition(line, character) |
| 166 | + ); |
| 167 | + goto endIndentDiagnose; |
| 168 | + } |
| 169 | + |
| 170 | + if (indentState.Style == IndentStyle::Space) |
| 171 | + { |
| 172 | + if (state.SpaceIndent != indentState.SpaceIndent) |
| 173 | + { |
| 174 | + ctx.PushDiagnosis( |
| 175 | + format(LText("incorrect indentation {}, here need {} space indentation"), |
| 176 | + indentState.SpaceIndent, state.SpaceIndent), |
| 177 | + LuaDiagnosisPosition(line, 0), |
| 178 | + LuaDiagnosisPosition(line, character) |
| 179 | + ); |
| 180 | + } |
| 181 | + goto endIndentDiagnose; |
| 182 | + } |
| 183 | + else |
| 184 | + { |
| 185 | + if (state.SpaceIndent != indentState.SpaceIndent || state.TabIndent != indentState.TabIndent) |
| 186 | + { |
| 187 | + ctx.PushDiagnosis( |
| 188 | + format(LText("incorrect indentation, here need {} tab and {} space indentation"), |
| 189 | + state.TabIndent, state.SpaceIndent), |
| 190 | + LuaDiagnosisPosition(line, 0), |
| 191 | + LuaDiagnosisPosition(line, character) |
| 192 | + ); |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + endIndentDiagnose: |
| 198 | + child->Diagnosis(ctx, it, *this); |
| 199 | + } |
| 200 | +} |
| 201 | + |
135 | 202 | void FormatElement::CopyFrom(std::shared_ptr<FormatElement> node) |
136 | 203 | { |
137 | 204 | _textRange = node->_textRange; |
|
0 commit comments