Skip to content

Commit e6307a1

Browse files
committedDec 24, 2024··
refactor(shirelang): simplify file path handling #170
- Introduce `relativePath` function in `shirecore` to replace redundant path calculations. - Consolidate file path logic across multiple files for consistency. - Update `PatternSearcher` to use `URLUtil.FILE_PROTOCOL` for file system refresh.
1 parent 81a93dd commit e6307a1

File tree

4 files changed

+17
-21
lines changed

4 files changed

+17
-21
lines changed
 

‎core/src/main/kotlin/com/phodal/shirecore/ProjectUtil.kt

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.intellij.openapi.fileTypes.FileTypeManager
66
import com.intellij.openapi.project.Project
77
import com.intellij.openapi.project.guessProjectDir
88
import com.intellij.openapi.util.ModificationTracker
9+
import com.intellij.openapi.util.io.FileUtil
910
import com.intellij.openapi.util.io.FileUtilRt
1011
import com.intellij.openapi.vcs.changes.VcsIgnoreManager
1112
import com.intellij.openapi.vfs.VirtualFile
@@ -46,6 +47,12 @@ fun VirtualFile.canBeAdded(project: Project): Boolean {
4647
return true
4748
}
4849

50+
fun VirtualFile.relativePath(project: Project): String {
51+
val projectDir = project.guessProjectDir()!!.toNioPath().toFile()
52+
val relativePath = FileUtil.getRelativePath(projectDir, this.toNioPath().toFile())
53+
return relativePath ?: this.path
54+
}
55+
4956
fun isIgnoredByVcs(project: Project?, file: VirtualFile?): Boolean {
5057
val ignoreManager = VcsIgnoreManager.getInstance(project!!)
5158
return ignoreManager.isPotentiallyIgnoredFile(file!!)

‎core/src/main/kotlin/com/phodal/shirecore/ui/input/ShireInput.kt

+2-8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import com.intellij.icons.AllIcons
2525
import com.intellij.ui.components.JBScrollPane
2626
import javax.swing.border.EmptyBorder
2727
import com.intellij.util.ui.JBUI
28+
import com.phodal.shirecore.relativePath
2829
import java.awt.event.MouseAdapter
2930
import java.awt.event.MouseEvent
3031

@@ -92,14 +93,7 @@ class ShireInput(val project: Project) : JPanel(BorderLayout()), Disposable {
9293
listModel.remove(index)
9394
} else {
9495
element.containingFile?.let { psiFile ->
95-
val virtualFile = psiFile.virtualFile
96-
val projectBasePath = project.basePath
97-
val filePath = virtualFile.path
98-
val relativePath = if (projectBasePath != null && filePath.startsWith(projectBasePath)) {
99-
filePath.substring(projectBasePath.length + 1)
100-
} else {
101-
filePath
102-
}
96+
val relativePath = psiFile.virtualFile.relativePath(project)
10397
inputSection.appendText("\n/file:${relativePath}")
10498
}
10599
}

‎shirelang/src/main/kotlin/com/phodal/shirelang/compiler/execute/command/FileShireCommand.kt

+4-10
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import com.intellij.lang.LanguageCommenters
44
import com.phodal.shirelang.compiler.ast.LineInfo
55
import com.intellij.openapi.diagnostic.logger
66
import com.intellij.openapi.project.Project
7-
import com.intellij.openapi.project.guessProjectDir
8-
import com.intellij.openapi.util.io.FileUtil
97
import com.intellij.openapi.vfs.VirtualFile
108
import com.intellij.psi.PsiManager
119
import com.phodal.shirecore.lookupFile
10+
import com.phodal.shirecore.relativePath
1211

1312
/**
1413
* FileAutoCommand is responsible for reading a file and returning its contents.
@@ -32,14 +31,9 @@ class FileShireCommand(private val myProject: Project, private val prop: String)
3231

3332
val fileContent = LineInfo.fromString(prop)?.splitContent(content) ?: content
3433
val language = PsiManager.getInstance(myProject).findFile(virtualFile)?.language
35-
val lang = language
36-
?.displayName
37-
?: "plaintext"
38-
39-
val relativePath = FileUtil.getRelativePath(
40-
myProject.guessProjectDir()!!.toNioPath().toFile(),
41-
virtualFile.toNioPath().toFile()
42-
)
34+
val lang = language?.displayName ?: "plaintext"
35+
36+
val relativePath = virtualFile.relativePath(myProject)
4337

4438
val commentPrefix = language?.let { LanguageCommenters.INSTANCE.forLanguage(it).lineCommentPrefix } ?: "//"
4539

‎shirelang/src/main/kotlin/com/phodal/shirelang/compiler/execute/searcher/PatternSearcher.kt

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.intellij.openapi.vfs.VfsUtilCore
77
import com.intellij.openapi.vfs.VirtualFile
88
import com.intellij.openapi.vfs.VirtualFileManager
99
import com.intellij.openapi.vfs.VirtualFileVisitor
10+
import com.intellij.util.io.URLUtil
1011
import java.util.regex.Pattern
1112

1213
object PatternSearcher {
@@ -27,8 +28,8 @@ object PatternSearcher {
2728
* @return A list of VirtualFile objects that match the specified regular expression. If no matching files are found, an empty list is returned.
2829
*/
2930
fun findFilesByRegex(project: Project, regex: String): List<VirtualFile> {
30-
if (PatternSearcher.cache.containsKey(regex)) {
31-
return PatternSearcher.cache[regex]!!
31+
if (cache.containsKey(regex)) {
32+
return cache[regex]!!
3233
}
3334

3435
val pattern: Pattern = try {
@@ -42,7 +43,7 @@ object PatternSearcher {
4243

4344
val matchingFiles: MutableList<VirtualFile> = ArrayList()
4445

45-
VirtualFileManager.getInstance().getFileSystem("file").refresh(false)
46+
VirtualFileManager.getInstance().getFileSystem(URLUtil.FILE_PROTOCOL).refresh(false)
4647

4748
VfsUtilCore.visitChildrenRecursively(baseDir, object : VirtualFileVisitor<Any>() {
4849
override fun visitFile(file: VirtualFile): Boolean {

0 commit comments

Comments
 (0)
Please sign in to comment.