|
| 1 | +#include "CodeService/FormatElement/SpaceElement.h" |
| 2 | + |
| 3 | +#include "CodeService/FormatElement/KeyWordElement.h" |
| 4 | +#include "CodeService/FormatElement/OperatorElement.h" |
| 5 | +#include "Util/format.h" |
| 6 | + |
| 7 | +SpaceElement::SpaceElement(int space) |
| 8 | + : FormatElement(), |
| 9 | + _space(space) |
| 10 | +{ |
| 11 | +} |
| 12 | + |
| 13 | +FormatElementType SpaceElement::GetType() |
| 14 | +{ |
| 15 | + return FormatElementType::SpaceElement; |
| 16 | +} |
| 17 | + |
| 18 | +void SpaceElement::Serialize(SerializeContext& ctx, ChildIterator selfIt, FormatElement& parent) |
| 19 | +{ |
| 20 | + int nextOffset = GetNextValidOffset(selfIt, parent); |
| 21 | + if (nextOffset != -1) |
| 22 | + { |
| 23 | + ctx.PrintBlank(_space); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +void SpaceElement::Diagnosis(DiagnosisContext& ctx, ChildIterator selfIt, FormatElement& parent) |
| 28 | +{ |
| 29 | + const int lastOffset = GetLastValidOffset(selfIt, parent); |
| 30 | + const int nextOffset = GetNextValidOffset(selfIt, parent); |
| 31 | + |
| 32 | + if (nextOffset == -1 && lastOffset != -1) |
| 33 | + { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + const int lastElementLine = ctx.GetLine(lastOffset); |
| 38 | + const int nextElementLine = ctx.GetLine(nextOffset); |
| 39 | + |
| 40 | + if (nextElementLine == lastElementLine) |
| 41 | + { |
| 42 | + PushSpaceDiagnostic(ctx, GetLastValidElement(selfIt, parent), GetNextValidElement(selfIt, parent)); |
| 43 | + } |
| 44 | + else |
| 45 | + { |
| 46 | + auto character = ctx.GetColumn(lastOffset); |
| 47 | + ctx.PushDiagnosis(LText("should be whitespace"), |
| 48 | + LuaDiagnosisPosition(lastElementLine, character + 1), |
| 49 | + LuaDiagnosisPosition(lastElementLine, character + 1), |
| 50 | + DiagnosisType::Blank); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +void SpaceElement::PushSpaceDiagnostic(DiagnosisContext& ctx, |
| 55 | + std::shared_ptr<FormatElement> left, |
| 56 | + std::shared_ptr<FormatElement> right) |
| 57 | +{ |
| 58 | + if (left == nullptr || right == nullptr) |
| 59 | + { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + auto leftOffset = left->GetTextRange().EndOffset; |
| 64 | + auto rightOffset = right->GetTextRange().StartOffset; |
| 65 | + int diff = rightOffset - leftOffset - 1; |
| 66 | + |
| 67 | + if (diff < 0 || _space < 0 || diff == _space) |
| 68 | + { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + auto additional = GetAdditionalNote(left, right); |
| 73 | + switch (_space) |
| 74 | + { |
| 75 | + case 0: |
| 76 | + { |
| 77 | + ctx.PushDiagnosis(Util::format(LText("unnecessary whitespace {}"), additional), |
| 78 | + TextRange(leftOffset, rightOffset), |
| 79 | + DiagnosisType::Blank); |
| 80 | + break; |
| 81 | + } |
| 82 | + case 1: |
| 83 | + { |
| 84 | + if (diff == 0) |
| 85 | + { |
| 86 | + ctx.PushDiagnosis(Util::format(LText("missing whitespace {}"), additional), |
| 87 | + TextRange(leftOffset, rightOffset), |
| 88 | + DiagnosisType::Blank); |
| 89 | + } |
| 90 | + else // diff > 1 |
| 91 | + { |
| 92 | + ctx.PushDiagnosis(Util::format(LText("multiple spaces {}"), additional), |
| 93 | + TextRange(leftOffset + 1, rightOffset - 1), |
| 94 | + DiagnosisType::Blank); |
| 95 | + } |
| 96 | + break; |
| 97 | + } |
| 98 | + default: |
| 99 | + { |
| 100 | + if (diff < _space) |
| 101 | + { |
| 102 | + ctx.PushDiagnosis(Util::format(LText("expected {} whitespace, found {} {}"), _space, diff, additional), |
| 103 | + TextRange(leftOffset, rightOffset), |
| 104 | + DiagnosisType::Blank); |
| 105 | + } |
| 106 | + else // diff > _space |
| 107 | + { |
| 108 | + ctx.PushDiagnosis(Util::format(LText("expected {} whitespace, found {} {}"), _space, diff, additional), |
| 109 | + TextRange(leftOffset + 1, rightOffset - 1), |
| 110 | + DiagnosisType::Blank); |
| 111 | + } |
| 112 | + break; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +std::string SpaceElement::GetAdditionalNote(std::shared_ptr<FormatElement> left, std::shared_ptr<FormatElement> right) |
| 118 | +{ |
| 119 | + if (left->Is(FormatElementType::KeyWordElement)) |
| 120 | + { |
| 121 | + auto keyWord = std::dynamic_pointer_cast<KeyWordElement>(left); |
| 122 | + return Util::format(LText("after keyword '{}'"), keyWord->GetText()); |
| 123 | + } |
| 124 | + else if (left->Is(FormatElementType::OperatorElement)) |
| 125 | + { |
| 126 | + auto operatorElement = std::dynamic_pointer_cast<OperatorElement>(left); |
| 127 | + return Util::format(LText("after operator '{}'"), operatorElement->GetText()); |
| 128 | + } |
| 129 | + else if (right->Is(FormatElementType::KeyWordElement)) |
| 130 | + { |
| 131 | + auto keyWord = std::dynamic_pointer_cast<KeyWordElement>(right); |
| 132 | + return Util::format(LText("before keyword '{}'"), keyWord->GetText()); |
| 133 | + } |
| 134 | + else if (right->Is(FormatElementType::OperatorElement)) |
| 135 | + { |
| 136 | + auto operatorElement = std::dynamic_pointer_cast<OperatorElement>(right); |
| 137 | + return Util::format(LText("before operator '{}'"), operatorElement->GetText()); |
| 138 | + } |
| 139 | + else |
| 140 | + { |
| 141 | + return ""; |
| 142 | + } |
| 143 | +} |
0 commit comments