diff options
| author | Yong He <yonghe@outlook.com> | 2024-08-12 20:53:03 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-12 20:53:03 -0700 |
| commit | b390566b55700582321b09b72c726b8dff9bd819 (patch) | |
| tree | a2fd8e50fcbde29dd2651e08a78021f2ae9d72de /source/slang/slang-workspace-version.cpp | |
| parent | 20bd48659d0009de5477380c335e2419f4c66f8b (diff) | |
Support unicode identifier names. (#4772)
* Support unicode identifier names.
* Fix.
* Fix language server.
* Fix build errors.
* Fix.
* Fix offset translation in language server.
Diffstat (limited to 'source/slang/slang-workspace-version.cpp')
| -rw-r--r-- | source/slang/slang-workspace-version.cpp | 87 |
1 files changed, 60 insertions, 27 deletions
diff --git a/source/slang/slang-workspace-version.cpp b/source/slang/slang-workspace-version.cpp index a07ef75cc..d85724328 100644 --- a/source/slang/slang-workspace-version.cpp +++ b/source/slang/slang-workspace-version.cpp @@ -396,42 +396,66 @@ void DocumentVersion::setText(const String& newText) { text = newText; StringUtil::calcLines(text.getUnownedSlice(), lines); - utf16CharStarts.clear(); + mapUTF16CharIndexToCodePointIndex.clear(); + mapCodePointIndexToUTF8ByteOffset.clear(); } -ArrayView<Index> DocumentVersion::getUTF16Boundaries(Index line) + +void DocumentVersion::ensureUTFBoundsAvailable() { - if (!utf16CharStarts.getCount()) + for (auto slice : lines) { - for (auto slice : lines) + List<Index> bounds; + List<Index> utf8Bounds; + Index index = 0; + Index codePointIndex = 0; + while (index < slice.getLength()) { - List<Index> bounds; - Index index = 0; - while (index < slice.getLength()) - { - auto startIndex = index; - const Char32 codePoint = getUnicodePointFromUTF8( - [&]() -> Byte - { - if (index < slice.getLength()) - return slice[index++]; - else - return '\0'; - }); - if (!codePoint) - break; - Char16 buffer[2]; - int count = encodeUnicodePointToUTF16Reversed(codePoint, buffer); - for (int i = 0; i < count; i++) - bounds.add(startIndex); - } - bounds.add(slice.getLength()); - utf16CharStarts.add(_Move(bounds)); + auto startIndex = index; + const Char32 codePoint = getUnicodePointFromUTF8( + [&]() -> Byte + { + if (index < slice.getLength()) + return slice[index++]; + else + return '\0'; + }); + if (!codePoint) + break; + + Char16 buffer[2]; + int count = encodeUnicodePointToUTF16Reversed(codePoint, buffer); + for (int i = 0; i < count; i++) + bounds.add(codePointIndex); + utf8Bounds.add(startIndex); + codePointIndex++; } + bounds.add(slice.getLength()); + utf8Bounds.add(slice.getLength()); + mapUTF16CharIndexToCodePointIndex.add(_Move(bounds)); + mapCodePointIndexToUTF8ByteOffset.add(_Move(utf8Bounds)); } - return line >= 1 && line <= utf16CharStarts.getCount() ? utf16CharStarts[line - 1].getArrayView() +} + +ArrayView<Index> DocumentVersion::getUTF16Boundaries(Index line) +{ + if (!mapUTF16CharIndexToCodePointIndex.getCount()) + { + ensureUTFBoundsAvailable(); + } + return line >= 1 && line <= mapUTF16CharIndexToCodePointIndex.getCount() ? mapUTF16CharIndexToCodePointIndex[line - 1].getArrayView() : ArrayView<Index>(); } +ArrayView<Index> DocumentVersion::getUTF8Boundaries(Index line) +{ + if (!mapCodePointIndexToUTF8ByteOffset.getCount()) + { + ensureUTFBoundsAvailable(); + } + return line >= 1 && line <= mapCodePointIndexToUTF8ByteOffset.getCount() ? mapCodePointIndexToUTF8ByteOffset[line - 1].getArrayView() + : ArrayView<Index>(); +} + void DocumentVersion::oneBasedUTF8LocToZeroBasedUTF16Loc( Index inLine, Index inCol, Index& outLine, Index& outCol) { @@ -447,6 +471,15 @@ void DocumentVersion::oneBasedUTF8LocToZeroBasedUTF16Loc( outCol = std::lower_bound(bounds.begin(), bounds.end(), inCol - 1) - bounds.begin(); } +void DocumentVersion::oneBasedUTF8LocToZeroBasedUTF16Loc( + Index inLine, Index inCol, int& outLine, int& outCol) +{ + Index ioutLine, ioutCol; + oneBasedUTF8LocToZeroBasedUTF16Loc(inLine, inCol, ioutLine, ioutCol); + outLine = (int)ioutLine; + outCol = (int)ioutCol; +} + void DocumentVersion::zeroBasedUTF16LocToOneBasedUTF8Loc( Index inLine, Index inCol, Index& outLine, Index& outCol) { |
