Skip to content

Commit

Permalink
Merge pull request #3781 from Hannah-Sten/create-file-quickfix-name
Browse files Browse the repository at this point in the history
Show formatted file path in file not found inspection quickfix name
  • Loading branch information
PHPirates authored Dec 4, 2024
2 parents 04f07ce + 3320304 commit f6a6e54
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased]

### Added
* Show formatted file path in file not found inspection quickfix name
* Automatically index bibliography files outside the project that are included by an absolute path
* Disable quotes inspection when TeX ligatures are disabled by fontspec
* Inspections can now be suppressed for any single line, or block of text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ open class LatexFileNotFoundInspection : TexifyInspectionBase() {
*/
class CreateNewFileWithDialogQuickFix(private val filePath: String, private val extension: String, private val elementPointer: SmartPsiElementPointer<LatexCommands>, private val key: String, private val range: TextRange) : LocalQuickFix {

override fun getFamilyName() = "Create file ${filePath.appendExtension(extension)}"
override fun getFamilyName() = "Create file ${filePath.appendExtension(extension).formatAsFilePath()}"

override fun startInWriteAction() = false

Expand Down
13 changes: 2 additions & 11 deletions src/nl/hannahsten/texifyidea/ui/CreateFileDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package nl.hannahsten.texifyidea.ui

import com.intellij.openapi.fileChooser.FileChooserDescriptor
import com.intellij.openapi.ui.*
import com.intellij.openapi.vfs.LocalFileSystem
import nl.hannahsten.texifyidea.util.formatAsFilePath
import nl.hannahsten.texifyidea.util.formatAsFilePathWithExistingParents
import java.io.File
import javax.swing.JPanel
import javax.swing.JTextField
Expand All @@ -26,17 +26,8 @@ class CreateFileDialog(private val basePath: String?, private val newFileName: S
panel.layout = VerticalFlowLayout(VerticalFlowLayout.TOP)

// Field to enter the name of the new file.
// If only the file is new, but the directory exists, use the existing directory and don't change it to follow conventions
val formattedPath = if (basePath != null) {
var existingPath = LocalFileSystem.getInstance().findFileByPath(basePath)
var existingRelativePath = ""
val partsToFormat = newFileName.split('/').dropWhile { part ->
if (existingPath?.exists() == false) return@dropWhile false
existingPath = existingPath?.children?.firstOrNull { it.name == part } ?: return@dropWhile false
existingRelativePath += "$part/"
true
}
existingRelativePath + partsToFormat.joinToString("/").formatAsFilePath()
formatAsFilePathWithExistingParents(basePath, newFileName)
}
else {
newFileName.formatAsFilePath()
Expand Down
16 changes: 16 additions & 0 deletions src/nl/hannahsten/texifyidea/util/Strings.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nl.hannahsten.texifyidea.util

import com.intellij.openapi.util.TextRange
import com.intellij.openapi.vfs.LocalFileSystem
import nl.hannahsten.texifyidea.util.magic.PatternMagic
import org.intellij.lang.annotations.Language
import java.io.File
Expand Down Expand Up @@ -175,6 +176,21 @@ fun String.formatAsFilePath(): String {
return formatted.ifEmpty { "myfile" }
}

/**
* If only the file is new, but the directory exists, use the existing directory and don't change it to follow conventions
*/
fun formatAsFilePathWithExistingParents(basePath: String, newFileName: String): String {
var existingPath = LocalFileSystem.getInstance().findFileByPath(basePath)
var existingRelativePath = ""
val partsToFormat = newFileName.split('/').dropWhile { part ->
if (existingPath?.exists() == false) return@dropWhile false
existingPath = existingPath?.children?.firstOrNull { it.name == part } ?: return@dropWhile false
existingRelativePath += "$part/"
true
}
return existingRelativePath + partsToFormat.joinToString("/").formatAsFilePath()
}

/**
* Formats the string as a valid LaTeX label name.
*/
Expand Down

0 comments on commit f6a6e54

Please sign in to comment.