fix: keep comments and layout when the editor saves a YAML file - #16
Merged
Merged
Conversation
Failing tests for the bug: every save through EditableKeyValueDataSource / EditableSingleDataSource -> repository rewrites the file from objects, so header, row, section, trailing and in-list comments all disappear. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
KeyValueDataRepository and SingleDataRepository rebuilt YAML files from objects on every save, so the editor erased every comment a designer wrote. YamlCommentPreserver diffs the canonical rendering of the file as it is on disk against the canonical rendering of the data being saved and replays only the differences onto the original text: untouched rows stay byte for byte, changed scalars are patched in place (keeping trailing comments and quote style), flow lists stay flow, rows match by Id so deletes and reorders take their own comments with them, and new rows follow the file's spacing. The result is re-read and compared with what the plain save would load as; anything it cannot patch falls back to the plain serializer output, so data is never at risk. CSV and JSON are untouched. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…L comments AssetDatabaseRawDataProvider reads the file back through the AssetDatabase at save time; this saves twice (the second time after the re-import) and checks the file byte for byte. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Data Editor 에서 YAML 테이블을 저장하면 파일에 적어 둔
#주석이 전부 사라졌습니다.KeyValueDataRepository/SingleDataRepository가 저장할 때마다 객체에서 문서를 통째로 다시 만들고, YamlDotNet 객체 모델에는 주석을 둘 자리가 없기 때문입니다. Spill 에서는 이 때문에 값 설명을 별도 문서로 옮겨야 했고, Breakline 도 같은 길을 가게 됩니다.방식
객체 모델이 주석을 들고 다니게 만드는 대신, 저장 시점에 텍스트 세 개를 비교합니다.
뒤의 둘을 구조 단위로 비교해 바뀐 부분만 원본 텍스트에 되돌려 씁니다 (
Datra/Serializers/YamlCommentPreserver.cs).[1, 2]),3.0같은 표기, 타입에 없는 키까지 남습니다.Id로 짝을 맞춥니다. 삭제된 행은 자기 주석(바로 위, 행 안, 줄 끝)을 데리고 사라지고, 순서가 바뀌면 주석도 같이 움직입니다. Id 가 없는 리스트는 안 바뀐 항목을 기준(LCS)으로 맞춥니다.라운드트립 모델(주석을 노드에 붙여 들고 다니는 방식)은 YamlDotNet 직렬화기를 대체해야 하고 polymorphic
$type·커스텀 컨버터와 두 번 맞춰야 해서 택하지 않았습니다. 이 방식은 기존 직렬화기를 그대로 두고 그 출력만 비교하므로 컨버터나 생성된 serializer 가 무엇이든 똑같이 동작합니다.안전장치: 합친 결과를 다시 읽어 직렬화한 값이 기존 방식으로 저장했을 때와 다르면, 또는 패치할 수 없는 구조(여러 문서, 파싱 불가 등)면 예전과 똑같은 정규 텍스트를 씁니다. 버그가 있어도 잃는 건 주석이지 데이터가 아닙니다. 아무것도 안 바뀐 저장(강제 저장 포함)은 파일을 그대로 둡니다.
범위
IsYaml()로 분기, JSON 은 테스트로 확인).AssetDatabaseRawDataProvider), WebEditor, 공유 에디터 레이어 모두EditableKeyValueDataSource/EditableSingleDataSource→ repositorySaveAsync()를 거치므로 별도 writer 없이 이 경로를 탑니다.SerializeYaml/IDataSerializer.SerializeTable은 원본 파일을 모르므로 그대로입니다.MultiFileKeyValueDataRepository(저장 미구현),AssetRepository도 이번 범위 밖입니다.테스트
Datra.Tests/YamlCommentPreservationTests.cs(21) — 에디터 저장 경로(EditableKeyValueDataSource/EditableSingleDataSource→ repository). 먼저 실패하는 테스트로 커밋(4b0b361, 20개 실패)한 뒤 수정했습니다.GameDataContext의 polymorphicSkills.yaml, JSON 테이블 동작 불변, 3000행 테이블Datra.Tests/YamlCommentPreserverTests.cs(14) — 저장 API 로는 만들 수 없는 경우: 행 재정렬, Id 없는 리스트 중간 삭제, 중첩 객체, 여러 줄 문자열, literal block 교체, 빈 필드 채우기, 타입에 없는 키 유지, 빈 테이블/전체 삭제,---, anchor/alias, 여러 문서·파싱 불가 시 폴백Datra.Unity.Sample/Assets/Tests/Editor/YamlCommentPreservationTests.cs—AssetDatabaseRawDataProvider로 두 번 저장(두 번째는 재임포트된 TextAsset 을 다시 읽음). Unity 6000.7.0b2 최소 프로젝트(이 브랜치의 Datra / Datra.Editor / Datra.Unity 를 file: 로 참조)에서 EditMode 통과. 샘플 프로젝트 자체(6000.2.6f2)는 로컬에 해당 에디터가 없어 돌리지 못했습니다.dotnet test Datra.sln -c Release: Datra.Tests 772, Datra.Editor.Tests 158, Datra.WebEditor.Tests 37, Datra.Analyzers.Tests 7 모두 통과.소비처
Spill / Breakline 은
com.penspanic.datra,com.penspanic.datra.editor,com.penspanic.datra.unity세 패키지를 머지 커밋으로 함께 올리면 됩니다. 수정은Datra패키지 안에 있고(Generator DLL 변경 없음), 데이터 파일 형식이나 API 변경은 없습니다. 새.cs에는.meta를 같이 넣었습니다.🤖 Generated with Claude Code