Skip to content

Commit e9ef6f9

Browse files
committed
refactor: improve logic for project-root co-authors file discovery
1 parent 4c864d1 commit e9ef6f9

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

src/main/kotlin/com/github/lppedd/cc/configuration/CCTokensService.kt

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,10 @@ internal class CCTokensService(private val project: Project) {
6060
fun getTokens(): TokensResult {
6161
val configService = CCConfigService.getInstance(project)
6262
val filePath = configService.customFilePath
63-
64-
val file = if (filePath != null) {
65-
LocalFileSystem.getInstance().refreshAndFindFileByPath(filePath)
66-
} else {
63+
val file = if (filePath == null) {
6764
findFileUnderProjectRoot(CC.File.Defaults)
65+
} else {
66+
LocalFileSystem.getInstance().refreshAndFindFileByPath(filePath)
6867
}
6968

7069
return if (file != null) {
@@ -97,11 +96,10 @@ internal class CCTokensService(private val project: Project) {
9796
fun getCoAuthors(): CoAuthorsResult {
9897
val configService = CCConfigService.getInstance(project)
9998
val filePath = configService.customCoAuthorsFilePath
100-
101-
val file = if (filePath != null) {
102-
LocalFileSystem.getInstance().refreshAndFindFileByPath(filePath)
99+
val file = if (filePath == null) {
100+
findFileUnderProjectRoot(CC.File.CoAuthors) ?: return CoAuthorsResult.Success(emptySet())
103101
} else {
104-
findFileUnderProjectRoot(CC.File.CoAuthors)
102+
LocalFileSystem.getInstance().refreshAndFindFileByPath(filePath)
105103
}
106104

107105
if (file == null) {
@@ -130,11 +128,21 @@ internal class CCTokensService(private val project: Project) {
130128
fun setCoAuthors(coAuthors: Set<String>): CoAuthorsResult {
131129
val configService = CCConfigService.getInstance(project)
132130
val filePath = configService.customCoAuthorsFilePath
133-
134-
val file = if (filePath != null) {
135-
LocalFileSystem.getInstance().refreshAndFindFileByPath(filePath)
131+
val file = if (filePath == null) {
132+
val file = findFileUnderProjectRoot(CC.File.CoAuthors)
133+
134+
if (file == null) {
135+
// Avoid creating an empty file if the co-authors set is empty and the file doesn't exist already
136+
if (coAuthors.isEmpty()) {
137+
return CoAuthorsResult.Success(emptySet())
138+
}
139+
140+
findFileUnderProjectRoot(CC.File.CoAuthors, createIfNotExists = true)
141+
} else {
142+
file
143+
}
136144
} else {
137-
findFileUnderProjectRoot(CC.File.CoAuthors, createIfNotExists = true)
145+
LocalFileSystem.getInstance().refreshAndFindFileByPath(filePath)
138146
}
139147

140148
if (file == null) {

0 commit comments

Comments
 (0)