@@ -566,111 +566,6 @@ collectRefactoringsAtCursor(SourceFile *SF, unsigned Line, unsigned Column,
566
566
return collectRefactorings (Tok, /* ExcludeRename=*/ false );
567
567
}
568
568
569
- // / Given a cursor position, this function tries to collect a number literal
570
- // / expression immediately following the cursor.
571
- static NumberLiteralExpr *getTrailingNumberLiteral (ResolvedCursorInfoPtr Tok) {
572
- // This cursor must point to the start of an expression.
573
- auto ExprStartInfo = dyn_cast<ResolvedExprStartCursorInfo>(Tok);
574
- if (!ExprStartInfo)
575
- return nullptr ;
576
-
577
- // For every sub-expression, try to find the literal expression that matches
578
- // our criteria.
579
- class FindLiteralNumber : public ASTWalker {
580
- Expr * const parent;
581
-
582
- public:
583
- NumberLiteralExpr *found = nullptr ;
584
-
585
- explicit FindLiteralNumber (Expr *parent) : parent(parent) { }
586
-
587
- MacroWalking getMacroWalkingBehavior () const override {
588
- return MacroWalking::Arguments;
589
- }
590
-
591
- PreWalkResult<Expr *> walkToExprPre (Expr *expr) override {
592
- if (auto *literal = dyn_cast<NumberLiteralExpr>(expr)) {
593
- // The sub-expression must have the same start loc with the outermost
594
- // expression, i.e. the cursor position.
595
- if (!found &&
596
- parent->getStartLoc ().getOpaquePointerValue () ==
597
- expr->getStartLoc ().getOpaquePointerValue ()) {
598
- found = literal;
599
- }
600
- }
601
- return Action::SkipChildrenIf (found, expr);
602
- }
603
- };
604
-
605
- auto parent = ExprStartInfo->getTrailingExpr ();
606
- FindLiteralNumber finder (parent);
607
- parent->walk (finder);
608
- return finder.found ;
609
- }
610
-
611
- static std::string insertUnderscore (StringRef Text) {
612
- SmallString<64 > Buffer;
613
- llvm::raw_svector_ostream OS (Buffer);
614
- for (auto It = Text.begin (); It != Text.end (); ++It) {
615
- unsigned Distance = It - Text.begin ();
616
- if (Distance && !(Distance % 3 )) {
617
- OS << ' _' ;
618
- }
619
- OS << *It;
620
- }
621
- return OS.str ().str ();
622
- }
623
-
624
- void insertUnderscoreInDigits (StringRef Digits,
625
- raw_ostream &OS) {
626
- StringRef BeforePointRef, AfterPointRef;
627
- std::tie (BeforePointRef, AfterPointRef) = Digits.split (' .' );
628
-
629
- std::string BeforePoint (BeforePointRef);
630
- std::string AfterPoint (AfterPointRef);
631
-
632
- // Insert '_' for the part before the decimal point.
633
- std::reverse (BeforePoint.begin (), BeforePoint.end ());
634
- BeforePoint = insertUnderscore (BeforePoint);
635
- std::reverse (BeforePoint.begin (), BeforePoint.end ());
636
- OS << BeforePoint;
637
-
638
- // Insert '_' for the part after the decimal point, if necessary.
639
- if (!AfterPoint.empty ()) {
640
- OS << ' .' ;
641
- OS << insertUnderscore (AfterPoint);
642
- }
643
- }
644
-
645
- bool RefactoringActionSimplifyNumberLiteral::isApplicable (
646
- ResolvedCursorInfoPtr Tok, DiagnosticEngine &Diag) {
647
- if (auto *Literal = getTrailingNumberLiteral (Tok)) {
648
- SmallString<64 > Buffer;
649
- llvm::raw_svector_ostream OS (Buffer);
650
- StringRef Digits = Literal->getDigitsText ();
651
- insertUnderscoreInDigits (Digits, OS);
652
-
653
- // If inserting '_' results in a different digit sequence, this refactoring
654
- // is applicable.
655
- return OS.str () != Digits;
656
- }
657
- return false ;
658
- }
659
-
660
- bool RefactoringActionSimplifyNumberLiteral::performChange () {
661
- if (auto *Literal = getTrailingNumberLiteral (CursorInfo)) {
662
-
663
- EditorConsumerInsertStream OS (EditConsumer, SM,
664
- CharSourceRange (SM, Literal->getDigitsLoc (),
665
- Lexer::getLocForEndOfToken (SM,
666
- Literal->getEndLoc ())));
667
- StringRef Digits = Literal->getDigitsText ();
668
- insertUnderscoreInDigits (Digits, OS);
669
- return false ;
670
- }
671
- return true ;
672
- }
673
-
674
569
static CallExpr *findTrailingClosureTarget (SourceManager &SM,
675
570
ResolvedCursorInfoPtr CursorInfo) {
676
571
if (CursorInfo->getKind () == CursorInfoKind::StmtStart)
0 commit comments