[clang] [clang-tools-extra] [clangd] Fix: exclude insertions at end of CursorPlaceholder in formatting (PR #87746)

2024-06-06 Thread via cfe-commits

roife wrote:

ping

https://github.com/llvm/llvm-project/pull/87746
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [clangd] Fix: exclude insertions at end of CursorPlaceholder in formatting (PR #87746)

2024-04-06 Thread via cfe-commits

https://github.com/roife updated https://github.com/llvm/llvm-project/pull/87746

>From b89d456b4ef5ae2311e448b939639c59b9c1aa23 Mon Sep 17 00:00:00 2001
From: roife 
Date: Thu, 4 Apr 2024 14:54:08 +0800
Subject: [PATCH] [clangd] Fix: exclude insertions at the end of placeholder

This commit introduces a new param 'includeInsAtPos' for 
'getShiftedCodePosition',
which is defaulted to be true. If 'includeInsAtPos' is true, the insertion at 
the
'Position' will be counted in. Otherwise, the insertion will not be counted in.

Changes made:
- Added new param 'includeInsAtPos' to 'getShiftedCodePosition'.
- Set 'includeInsAtPos' to be false when calculate the end position of
  'CursorPlaceholder' in 'formatIncremental' of clangd.

Fixes #71983
---
 clang-tools-extra/clangd/Format.cpp   |  2 +-
 .../clangd/unittests/FormatTests.cpp  | 19 +++
 .../include/clang/Tooling/Core/Replacement.h  |  8 +---
 clang/lib/Tooling/Core/Replacement.cpp|  7 +--
 4 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/clang-tools-extra/clangd/Format.cpp 
b/clang-tools-extra/clangd/Format.cpp
index 272a34d4ed7972..901a709e2e3934 100644
--- a/clang-tools-extra/clangd/Format.cpp
+++ b/clang-tools-extra/clangd/Format.cpp
@@ -341,7 +341,7 @@ formatIncremental(llvm::StringRef OriginalCode, unsigned 
OriginalCursor,
   unsigned FormattedCursorStart =
FormattingChanges.getShiftedCodePosition(Cursor),
FormattedCursorEnd = FormattingChanges.getShiftedCodePosition(
-   Cursor + Incremental.CursorPlaceholder.size());
+   Cursor + Incremental.CursorPlaceholder.size(), false);
   tooling::Replacements RemoveCursorPlaceholder(
   tooling::Replacement(Filename, FormattedCursorStart,
FormattedCursorEnd - FormattedCursorStart, ""));
diff --git a/clang-tools-extra/clangd/unittests/FormatTests.cpp 
b/clang-tools-extra/clangd/unittests/FormatTests.cpp
index f7384a1bc63c99..72e662a07062d3 100644
--- a/clang-tools-extra/clangd/unittests/FormatTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FormatTests.cpp
@@ -315,6 +315,25 @@ vector x = {1, 2, 3}^
 )cpp");
 }
 
+TEST(FormatIncremental, InsertBraces) {
+  format::FormatStyle Style =
+  format::getGoogleStyle(format::FormatStyle::LK_Cpp);
+  Style.InsertBraces = true;
+  expectAfterNewline(R"cpp(
+int main() {
+  while (true)
+^
+}
+)cpp",
+ R"cpp(
+int main() {
+  while (true) {
+
+  }^
+}
+)cpp",
+ Style);
+}
 } // namespace
 } // namespace clangd
 } // namespace clang
diff --git a/clang/include/clang/Tooling/Core/Replacement.h 
b/clang/include/clang/Tooling/Core/Replacement.h
index f9452111e147f1..d2b79e4ab07ae7 100644
--- a/clang/include/clang/Tooling/Core/Replacement.h
+++ b/clang/include/clang/Tooling/Core/Replacement.h
@@ -268,9 +268,11 @@ class Replacements {
   std::vector getAffectedRanges() const;
 
   // Returns the new offset in the code after replacements being applied.
-  // Note that if there is an insertion at Offset in the current replacements,
-  // \p Offset will be shifted to Offset + Length in inserted text.
-  unsigned getShiftedCodePosition(unsigned Position) const;
+  // If \p includeInsAtPos is true and there is an insertion at Offset in the
+  // current replacements, \p Offset will be shifted to Offset + Length in
+  // inserted text. Otherwise, the insertion at Offset will not be counted in.
+  unsigned getShiftedCodePosition(unsigned Position,
+  bool includeInsAtPos = true) const;
 
   unsigned size() const { return Replaces.size(); }
 
diff --git a/clang/lib/Tooling/Core/Replacement.cpp 
b/clang/lib/Tooling/Core/Replacement.cpp
index 269f17a6db4cfc..07d3e4e58718cb 100644
--- a/clang/lib/Tooling/Core/Replacement.cpp
+++ b/clang/lib/Tooling/Core/Replacement.cpp
@@ -544,10 +544,13 @@ std::vector Replacements::getAffectedRanges() 
const {
   return combineAndSortRanges(ChangedRanges);
 }
 
-unsigned Replacements::getShiftedCodePosition(unsigned Position) const {
+unsigned Replacements::getShiftedCodePosition(unsigned Position,
+  bool includeInsAtPos) const {
   unsigned Offset = 0;
   for (const auto &R : Replaces) {
-if (R.getOffset() + R.getLength() <= Position) {
+unsigned End = R.getOffset() + R.getLength();
+if (End <= Position &&
+(includeInsAtPos || (End < Position || R.getLength() > 0))) {
   Offset += R.getReplacementText().size() - R.getLength();
   continue;
 }

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [clangd] Fix: exclude insertions at end of CursorPlaceholder in formatting (PR #87746)

2024-04-06 Thread Younan Zhang via cfe-commits

zyn0217 wrote:

AFAIK the Windows CI has been rather fragile recently, and I think you could 
probably ignore it.

https://github.com/llvm/llvm-project/pull/87746
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [clangd] Fix: exclude insertions at end of CursorPlaceholder in formatting (PR #87746)

2024-04-06 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff d4cd65ecf2546e509f43363f96364c976f49b9da 
03fe6bec808cb968c35f1bad7432ea6155ed0115 -- clang-tools-extra/clangd/Format.cpp 
clang-tools-extra/clangd/unittests/FormatTests.cpp 
clang/include/clang/Tooling/Core/Replacement.h 
clang/lib/Tooling/Core/Replacement.cpp
``





View the diff from clang-format here.


``diff
diff --git a/clang-tools-extra/clangd/unittests/FormatTests.cpp 
b/clang-tools-extra/clangd/unittests/FormatTests.cpp
index 22c19fefc6..72e662a070 100644
--- a/clang-tools-extra/clangd/unittests/FormatTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FormatTests.cpp
@@ -317,7 +317,7 @@ vector x = {1, 2, 3}^
 
 TEST(FormatIncremental, InsertBraces) {
   format::FormatStyle Style =
-format::getGoogleStyle(format::FormatStyle::LK_Cpp);
+  format::getGoogleStyle(format::FormatStyle::LK_Cpp);
   Style.InsertBraces = true;
   expectAfterNewline(R"cpp(
 int main() {
diff --git a/clang/include/clang/Tooling/Core/Replacement.h 
b/clang/include/clang/Tooling/Core/Replacement.h
index 69fe5186df..d2b79e4ab0 100644
--- a/clang/include/clang/Tooling/Core/Replacement.h
+++ b/clang/include/clang/Tooling/Core/Replacement.h
@@ -271,7 +271,8 @@ public:
   // If \p includeInsAtPos is true and there is an insertion at Offset in the
   // current replacements, \p Offset will be shifted to Offset + Length in
   // inserted text. Otherwise, the insertion at Offset will not be counted in.
-  unsigned getShiftedCodePosition(unsigned Position, bool includeInsAtPos = 
true) const;
+  unsigned getShiftedCodePosition(unsigned Position,
+  bool includeInsAtPos = true) const;
 
   unsigned size() const { return Replaces.size(); }
 
diff --git a/clang/lib/Tooling/Core/Replacement.cpp 
b/clang/lib/Tooling/Core/Replacement.cpp
index 95a3b62f47..07d3e4e587 100644
--- a/clang/lib/Tooling/Core/Replacement.cpp
+++ b/clang/lib/Tooling/Core/Replacement.cpp
@@ -549,8 +549,8 @@ unsigned Replacements::getShiftedCodePosition(unsigned 
Position,
   unsigned Offset = 0;
   for (const auto &R : Replaces) {
 unsigned End = R.getOffset() + R.getLength();
-if (End <= Position
-&& (includeInsAtPos || (End < Position || R.getLength() > 0))) {
+if (End <= Position &&
+(includeInsAtPos || (End < Position || R.getLength() > 0))) {
   Offset += R.getReplacementText().size() - R.getLength();
   continue;
 }

``




https://github.com/llvm/llvm-project/pull/87746
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [clangd] Fix: exclude insertions at end of CursorPlaceholder in formatting (PR #87746)

2024-04-06 Thread via cfe-commits

roife wrote:

I'm not sure why the test is showing as failed since all tests of clangd have 
passed; I didn't find any failed tests in the log.

https://github.com/llvm/llvm-project/pull/87746
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [clangd] Fix: exclude insertions at end of CursorPlaceholder in formatting (PR #87746)

2024-04-06 Thread via cfe-commits

https://github.com/roife updated https://github.com/llvm/llvm-project/pull/87746

>From 03fe6bec808cb968c35f1bad7432ea6155ed0115 Mon Sep 17 00:00:00 2001
From: roife 
Date: Thu, 4 Apr 2024 14:54:08 +0800
Subject: [PATCH] [clangd] Fix: exclude insertions at the end of
 CursorPlaceholder in formatting

This commit introduces a new param 'includeInsAtPos' for 
'getShiftedCodePosition',
which is defaulted to be true. If 'includeInsAtPos' is true, the insertion at 
the
'Position' will be counted in. Otherwise, the insertion will not be counted in.

Changes made:
- Added new param 'includeInsAtPos' to 'getShiftedCodePosition'.
- Set 'includeInsAtPos' to be false when calculate the end position of
  'CursorPlaceholder' in 'formatIncremental' of clangd.

Fixes #71983
---
 clang-tools-extra/clangd/Format.cpp   |  2 +-
 .../clangd/unittests/FormatTests.cpp  | 19 +++
 .../include/clang/Tooling/Core/Replacement.h  |  7 ---
 clang/lib/Tooling/Core/Replacement.cpp|  7 +--
 4 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/clang-tools-extra/clangd/Format.cpp 
b/clang-tools-extra/clangd/Format.cpp
index 272a34d4ed7972..901a709e2e3934 100644
--- a/clang-tools-extra/clangd/Format.cpp
+++ b/clang-tools-extra/clangd/Format.cpp
@@ -341,7 +341,7 @@ formatIncremental(llvm::StringRef OriginalCode, unsigned 
OriginalCursor,
   unsigned FormattedCursorStart =
FormattingChanges.getShiftedCodePosition(Cursor),
FormattedCursorEnd = FormattingChanges.getShiftedCodePosition(
-   Cursor + Incremental.CursorPlaceholder.size());
+   Cursor + Incremental.CursorPlaceholder.size(), false);
   tooling::Replacements RemoveCursorPlaceholder(
   tooling::Replacement(Filename, FormattedCursorStart,
FormattedCursorEnd - FormattedCursorStart, ""));
diff --git a/clang-tools-extra/clangd/unittests/FormatTests.cpp 
b/clang-tools-extra/clangd/unittests/FormatTests.cpp
index f7384a1bc63c99..22c19fefc61aad 100644
--- a/clang-tools-extra/clangd/unittests/FormatTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FormatTests.cpp
@@ -315,6 +315,25 @@ vector x = {1, 2, 3}^
 )cpp");
 }
 
+TEST(FormatIncremental, InsertBraces) {
+  format::FormatStyle Style =
+format::getGoogleStyle(format::FormatStyle::LK_Cpp);
+  Style.InsertBraces = true;
+  expectAfterNewline(R"cpp(
+int main() {
+  while (true)
+^
+}
+)cpp",
+ R"cpp(
+int main() {
+  while (true) {
+
+  }^
+}
+)cpp",
+ Style);
+}
 } // namespace
 } // namespace clangd
 } // namespace clang
diff --git a/clang/include/clang/Tooling/Core/Replacement.h 
b/clang/include/clang/Tooling/Core/Replacement.h
index f9452111e147f1..69fe5186dfb0a5 100644
--- a/clang/include/clang/Tooling/Core/Replacement.h
+++ b/clang/include/clang/Tooling/Core/Replacement.h
@@ -268,9 +268,10 @@ class Replacements {
   std::vector getAffectedRanges() const;
 
   // Returns the new offset in the code after replacements being applied.
-  // Note that if there is an insertion at Offset in the current replacements,
-  // \p Offset will be shifted to Offset + Length in inserted text.
-  unsigned getShiftedCodePosition(unsigned Position) const;
+  // If \p includeInsAtPos is true and there is an insertion at Offset in the
+  // current replacements, \p Offset will be shifted to Offset + Length in
+  // inserted text. Otherwise, the insertion at Offset will not be counted in.
+  unsigned getShiftedCodePosition(unsigned Position, bool includeInsAtPos = 
true) const;
 
   unsigned size() const { return Replaces.size(); }
 
diff --git a/clang/lib/Tooling/Core/Replacement.cpp 
b/clang/lib/Tooling/Core/Replacement.cpp
index 269f17a6db4cfc..95a3b62f47c708 100644
--- a/clang/lib/Tooling/Core/Replacement.cpp
+++ b/clang/lib/Tooling/Core/Replacement.cpp
@@ -544,10 +544,13 @@ std::vector Replacements::getAffectedRanges() 
const {
   return combineAndSortRanges(ChangedRanges);
 }
 
-unsigned Replacements::getShiftedCodePosition(unsigned Position) const {
+unsigned Replacements::getShiftedCodePosition(unsigned Position,
+  bool includeInsAtPos) const {
   unsigned Offset = 0;
   for (const auto &R : Replaces) {
-if (R.getOffset() + R.getLength() <= Position) {
+unsigned End = R.getOffset() + R.getLength();
+if (End <= Position
+&& (includeInsAtPos || (End < Position || R.getLength() > 0))) {
   Offset += R.getReplacementText().size() - R.getLength();
   continue;
 }

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [clangd] Fix: exclude insertions at end of CursorPlaceholder in formatting (PR #87746)

2024-04-04 Thread via cfe-commits

https://github.com/roife edited https://github.com/llvm/llvm-project/pull/87746
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [clangd] Fix: exclude insertions at end of CursorPlaceholder in formatting (PR #87746)

2024-04-04 Thread via cfe-commits

https://github.com/roife updated https://github.com/llvm/llvm-project/pull/87746

>From eabedb36dd3d97edc0973321470ac9f69b64f572 Mon Sep 17 00:00:00 2001
From: roife 
Date: Thu, 4 Apr 2024 14:54:08 +0800
Subject: [PATCH] [clangd] Fix: exclude insertions at end of CursorPlaceholder
 in formatting

This commit introduces a new param 'includeInsAtPos' for 
'getShiftedCodePosition',
which is defaulted to be true. If 'includeInsAtPos' is true, the insertion at 
the
'Position' will not be counted in.

Changes made:
- Added new param 'includeInsAtPos' to 'getShiftedCodePosition'.
- Set 'includeInsAtPos' to be false when calculate the end position of
  'CursorPlaceholder' in 'formatIncremental' of clangd.

Fixes #71983
---
 clang-tools-extra/clangd/Format.cpp   |  2 +-
 .../clangd/unittests/FormatTests.cpp  | 19 +++
 .../include/clang/Tooling/Core/Replacement.h  |  7 ---
 clang/lib/Tooling/Core/Replacement.cpp|  7 +--
 4 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/clang-tools-extra/clangd/Format.cpp 
b/clang-tools-extra/clangd/Format.cpp
index 272a34d4ed7972..901a709e2e3934 100644
--- a/clang-tools-extra/clangd/Format.cpp
+++ b/clang-tools-extra/clangd/Format.cpp
@@ -341,7 +341,7 @@ formatIncremental(llvm::StringRef OriginalCode, unsigned 
OriginalCursor,
   unsigned FormattedCursorStart =
FormattingChanges.getShiftedCodePosition(Cursor),
FormattedCursorEnd = FormattingChanges.getShiftedCodePosition(
-   Cursor + Incremental.CursorPlaceholder.size());
+   Cursor + Incremental.CursorPlaceholder.size(), false);
   tooling::Replacements RemoveCursorPlaceholder(
   tooling::Replacement(Filename, FormattedCursorStart,
FormattedCursorEnd - FormattedCursorStart, ""));
diff --git a/clang-tools-extra/clangd/unittests/FormatTests.cpp 
b/clang-tools-extra/clangd/unittests/FormatTests.cpp
index f7384a1bc63c99..22c19fefc61aad 100644
--- a/clang-tools-extra/clangd/unittests/FormatTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FormatTests.cpp
@@ -315,6 +315,25 @@ vector x = {1, 2, 3}^
 )cpp");
 }
 
+TEST(FormatIncremental, InsertBraces) {
+  format::FormatStyle Style =
+format::getGoogleStyle(format::FormatStyle::LK_Cpp);
+  Style.InsertBraces = true;
+  expectAfterNewline(R"cpp(
+int main() {
+  while (true)
+^
+}
+)cpp",
+ R"cpp(
+int main() {
+  while (true) {
+
+  }^
+}
+)cpp",
+ Style);
+}
 } // namespace
 } // namespace clangd
 } // namespace clang
diff --git a/clang/include/clang/Tooling/Core/Replacement.h 
b/clang/include/clang/Tooling/Core/Replacement.h
index f9452111e147f1..69fe5186dfb0a5 100644
--- a/clang/include/clang/Tooling/Core/Replacement.h
+++ b/clang/include/clang/Tooling/Core/Replacement.h
@@ -268,9 +268,10 @@ class Replacements {
   std::vector getAffectedRanges() const;
 
   // Returns the new offset in the code after replacements being applied.
-  // Note that if there is an insertion at Offset in the current replacements,
-  // \p Offset will be shifted to Offset + Length in inserted text.
-  unsigned getShiftedCodePosition(unsigned Position) const;
+  // If \p includeInsAtPos is true and there is an insertion at Offset in the
+  // current replacements, \p Offset will be shifted to Offset + Length in
+  // inserted text. Otherwise, the insertion at Offset will not be counted in.
+  unsigned getShiftedCodePosition(unsigned Position, bool includeInsAtPos = 
true) const;
 
   unsigned size() const { return Replaces.size(); }
 
diff --git a/clang/lib/Tooling/Core/Replacement.cpp 
b/clang/lib/Tooling/Core/Replacement.cpp
index 269f17a6db4cfc..95a3b62f47c708 100644
--- a/clang/lib/Tooling/Core/Replacement.cpp
+++ b/clang/lib/Tooling/Core/Replacement.cpp
@@ -544,10 +544,13 @@ std::vector Replacements::getAffectedRanges() 
const {
   return combineAndSortRanges(ChangedRanges);
 }
 
-unsigned Replacements::getShiftedCodePosition(unsigned Position) const {
+unsigned Replacements::getShiftedCodePosition(unsigned Position,
+  bool includeInsAtPos) const {
   unsigned Offset = 0;
   for (const auto &R : Replaces) {
-if (R.getOffset() + R.getLength() <= Position) {
+unsigned End = R.getOffset() + R.getLength();
+if (End <= Position
+&& (includeInsAtPos || (End < Position || R.getLength() > 0))) {
   Offset += R.getReplacementText().size() - R.getLength();
   continue;
 }

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [clangd] Fix: exclude insertions at end of CursorPlaceholder in formatting (PR #87746)

2024-04-04 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-tools-extra

Author: roife (roife)


Changes

Fixes #71983

This commit introduces a new param 'includeInsAtPos' for 
'getShiftedCodePosition', which is defaulted to be true. If 'includeInsAtPos' 
is true, the insertion at the 'Position' will not be counted in.

Changes made:
- Added new param 'includeInsAtPos' to 'getShiftedCodePosition'.
- Set 'includeInsAtPos' to be true when calculate the end position of 
'CursorPlaceholder' in 'formatIncremental' of clangd.

---
Full diff: https://github.com/llvm/llvm-project/pull/87746.diff


4 Files Affected:

- (modified) clang-tools-extra/clangd/Format.cpp (+1-1) 
- (modified) clang-tools-extra/clangd/unittests/FormatTests.cpp (+19) 
- (modified) clang/include/clang/Tooling/Core/Replacement.h (+4-3) 
- (modified) clang/lib/Tooling/Core/Replacement.cpp (+5-2) 


``diff
diff --git a/clang-tools-extra/clangd/Format.cpp 
b/clang-tools-extra/clangd/Format.cpp
index 272a34d4ed7972..901a709e2e3934 100644
--- a/clang-tools-extra/clangd/Format.cpp
+++ b/clang-tools-extra/clangd/Format.cpp
@@ -341,7 +341,7 @@ formatIncremental(llvm::StringRef OriginalCode, unsigned 
OriginalCursor,
   unsigned FormattedCursorStart =
FormattingChanges.getShiftedCodePosition(Cursor),
FormattedCursorEnd = FormattingChanges.getShiftedCodePosition(
-   Cursor + Incremental.CursorPlaceholder.size());
+   Cursor + Incremental.CursorPlaceholder.size(), false);
   tooling::Replacements RemoveCursorPlaceholder(
   tooling::Replacement(Filename, FormattedCursorStart,
FormattedCursorEnd - FormattedCursorStart, ""));
diff --git a/clang-tools-extra/clangd/unittests/FormatTests.cpp 
b/clang-tools-extra/clangd/unittests/FormatTests.cpp
index f7384a1bc63c99..22c19fefc61aad 100644
--- a/clang-tools-extra/clangd/unittests/FormatTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FormatTests.cpp
@@ -315,6 +315,25 @@ vector x = {1, 2, 3}^
 )cpp");
 }
 
+TEST(FormatIncremental, InsertBraces) {
+  format::FormatStyle Style =
+format::getGoogleStyle(format::FormatStyle::LK_Cpp);
+  Style.InsertBraces = true;
+  expectAfterNewline(R"cpp(
+int main() {
+  while (true)
+^
+}
+)cpp",
+ R"cpp(
+int main() {
+  while (true) {
+
+  }^
+}
+)cpp",
+ Style);
+}
 } // namespace
 } // namespace clangd
 } // namespace clang
diff --git a/clang/include/clang/Tooling/Core/Replacement.h 
b/clang/include/clang/Tooling/Core/Replacement.h
index f9452111e147f1..69fe5186dfb0a5 100644
--- a/clang/include/clang/Tooling/Core/Replacement.h
+++ b/clang/include/clang/Tooling/Core/Replacement.h
@@ -268,9 +268,10 @@ class Replacements {
   std::vector getAffectedRanges() const;
 
   // Returns the new offset in the code after replacements being applied.
-  // Note that if there is an insertion at Offset in the current replacements,
-  // \p Offset will be shifted to Offset + Length in inserted text.
-  unsigned getShiftedCodePosition(unsigned Position) const;
+  // If \p includeInsAtPos is true and there is an insertion at Offset in the
+  // current replacements, \p Offset will be shifted to Offset + Length in
+  // inserted text. Otherwise, the insertion at Offset will not be counted in.
+  unsigned getShiftedCodePosition(unsigned Position, bool includeInsAtPos = 
true) const;
 
   unsigned size() const { return Replaces.size(); }
 
diff --git a/clang/lib/Tooling/Core/Replacement.cpp 
b/clang/lib/Tooling/Core/Replacement.cpp
index 269f17a6db4cfc..95a3b62f47c708 100644
--- a/clang/lib/Tooling/Core/Replacement.cpp
+++ b/clang/lib/Tooling/Core/Replacement.cpp
@@ -544,10 +544,13 @@ std::vector Replacements::getAffectedRanges() 
const {
   return combineAndSortRanges(ChangedRanges);
 }
 
-unsigned Replacements::getShiftedCodePosition(unsigned Position) const {
+unsigned Replacements::getShiftedCodePosition(unsigned Position,
+  bool includeInsAtPos) const {
   unsigned Offset = 0;
   for (const auto &R : Replaces) {
-if (R.getOffset() + R.getLength() <= Position) {
+unsigned End = R.getOffset() + R.getLength();
+if (End <= Position
+&& (includeInsAtPos || (End < Position || R.getLength() > 0))) {
   Offset += R.getReplacementText().size() - R.getLength();
   continue;
 }

``




https://github.com/llvm/llvm-project/pull/87746
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [clangd] Fix: exclude insertions at end of CursorPlaceholder in formatting (PR #87746)

2024-04-04 Thread via cfe-commits

github-actions[bot] wrote:



Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this 
page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using `@` followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from 
other developers.

If you have further questions, they may be answered by the [LLVM GitHub User 
Guide](https://llvm.org/docs/GitHub.html).

You can also ask questions in a comment on this PR, on the [LLVM 
Discord](https://discord.com/invite/xS7Z362) or on the 
[forums](https://discourse.llvm.org/).

https://github.com/llvm/llvm-project/pull/87746
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [clangd] Fix: exclude insertions at end of CursorPlaceholder in formatting (PR #87746)

2024-04-04 Thread via cfe-commits

https://github.com/roife created https://github.com/llvm/llvm-project/pull/87746

Fixes #71983

This commit introduces a new param 'includeInsAtPos' for 
'getShiftedCodePosition', which is defaulted to be true. If 'includeInsAtPos' 
is true, the insertion at the 'Position' will not be counted in.

Changes made:
- Added new param 'includeInsAtPos' to 'getShiftedCodePosition'.
- Set 'includeInsAtPos' to be true when calculate the end position of 
'CursorPlaceholder' in 'formatIncremental' of clangd.

>From 38d910410b2eb8afbefc035b5c8dd04bee3cf34f Mon Sep 17 00:00:00 2001
From: roife 
Date: Thu, 4 Apr 2024 14:54:08 +0800
Subject: [PATCH] [clangd] Fix: exclude insertions at end of CursorPlaceholder
 in formatting

This commit introduces a new param 'includeInsAtPos' for 
'getShiftedCodePosition',
which is defaulted to be true. If 'includeInsAtPos' is true, the insertion at 
the
'Position' will not be counted in.

Changes made:
- Added new param 'includeInsAtPos' to 'getShiftedCodePosition'.
- Set 'includeInsAtPos' to be true when calculate the end position of
  'CursorPlaceholder' in 'formatIncremental' of clangd.

Fixes #71983
---
 clang-tools-extra/clangd/Format.cpp   |  2 +-
 .../clangd/unittests/FormatTests.cpp  | 19 +++
 .../include/clang/Tooling/Core/Replacement.h  |  7 ---
 clang/lib/Tooling/Core/Replacement.cpp|  7 +--
 4 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/clang-tools-extra/clangd/Format.cpp 
b/clang-tools-extra/clangd/Format.cpp
index 272a34d4ed7972..901a709e2e3934 100644
--- a/clang-tools-extra/clangd/Format.cpp
+++ b/clang-tools-extra/clangd/Format.cpp
@@ -341,7 +341,7 @@ formatIncremental(llvm::StringRef OriginalCode, unsigned 
OriginalCursor,
   unsigned FormattedCursorStart =
FormattingChanges.getShiftedCodePosition(Cursor),
FormattedCursorEnd = FormattingChanges.getShiftedCodePosition(
-   Cursor + Incremental.CursorPlaceholder.size());
+   Cursor + Incremental.CursorPlaceholder.size(), false);
   tooling::Replacements RemoveCursorPlaceholder(
   tooling::Replacement(Filename, FormattedCursorStart,
FormattedCursorEnd - FormattedCursorStart, ""));
diff --git a/clang-tools-extra/clangd/unittests/FormatTests.cpp 
b/clang-tools-extra/clangd/unittests/FormatTests.cpp
index f7384a1bc63c99..22c19fefc61aad 100644
--- a/clang-tools-extra/clangd/unittests/FormatTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FormatTests.cpp
@@ -315,6 +315,25 @@ vector x = {1, 2, 3}^
 )cpp");
 }
 
+TEST(FormatIncremental, InsertBraces) {
+  format::FormatStyle Style =
+format::getGoogleStyle(format::FormatStyle::LK_Cpp);
+  Style.InsertBraces = true;
+  expectAfterNewline(R"cpp(
+int main() {
+  while (true)
+^
+}
+)cpp",
+ R"cpp(
+int main() {
+  while (true) {
+
+  }^
+}
+)cpp",
+ Style);
+}
 } // namespace
 } // namespace clangd
 } // namespace clang
diff --git a/clang/include/clang/Tooling/Core/Replacement.h 
b/clang/include/clang/Tooling/Core/Replacement.h
index f9452111e147f1..69fe5186dfb0a5 100644
--- a/clang/include/clang/Tooling/Core/Replacement.h
+++ b/clang/include/clang/Tooling/Core/Replacement.h
@@ -268,9 +268,10 @@ class Replacements {
   std::vector getAffectedRanges() const;
 
   // Returns the new offset in the code after replacements being applied.
-  // Note that if there is an insertion at Offset in the current replacements,
-  // \p Offset will be shifted to Offset + Length in inserted text.
-  unsigned getShiftedCodePosition(unsigned Position) const;
+  // If \p includeInsAtPos is true and there is an insertion at Offset in the
+  // current replacements, \p Offset will be shifted to Offset + Length in
+  // inserted text. Otherwise, the insertion at Offset will not be counted in.
+  unsigned getShiftedCodePosition(unsigned Position, bool includeInsAtPos = 
true) const;
 
   unsigned size() const { return Replaces.size(); }
 
diff --git a/clang/lib/Tooling/Core/Replacement.cpp 
b/clang/lib/Tooling/Core/Replacement.cpp
index 269f17a6db4cfc..95a3b62f47c708 100644
--- a/clang/lib/Tooling/Core/Replacement.cpp
+++ b/clang/lib/Tooling/Core/Replacement.cpp
@@ -544,10 +544,13 @@ std::vector Replacements::getAffectedRanges() 
const {
   return combineAndSortRanges(ChangedRanges);
 }
 
-unsigned Replacements::getShiftedCodePosition(unsigned Position) const {
+unsigned Replacements::getShiftedCodePosition(unsigned Position,
+  bool includeInsAtPos) const {
   unsigned Offset = 0;
   for (const auto &R : Replaces) {
-if (R.getOffset() + R.getLength() <= Position) {
+unsigned End = R.getOffset() + R.getLength();
+if (End <= Position
+&& (includeInsAtPos || (End < Position || R.getLength() > 0))) {
   Offset += R.getReplacementText().size() - R.getLength();
   continue;
 }

___
cfe-commits