Skip to content

Commit

Permalink
Added an action to create a new empty file
Browse files Browse the repository at this point in the history
  • Loading branch information
kizeevov committed Nov 16, 2023
1 parent 70336e3 commit 5fe1550
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 21 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

## [Unreleased]

### Added
- Added an action to create a new empty file

### Fixed
- Fixed linear marker preview

## [0.2.7] - 2023-11-15

### Fixed
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package dev.slint.ideaplugin.ide.actions

import com.intellij.ide.actions.CreateFileFromTemplateAction
import com.intellij.ide.actions.CreateFileFromTemplateDialog
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.client.currentSession
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.psi.PsiDirectory
import dev.slint.ideaplugin.SlintBundle
import dev.slint.ideaplugin.SlintIcons
import org.jetbrains.annotations.Nls

class SlintCreateFileAction : CreateFileFromTemplateAction(CAPTION, "", SlintIcons.SLINT),
DumbAware {

override fun getActionName(directory: PsiDirectory?, newName: String, templateName: String?): String = CAPTION

override fun isAvailable(dataContext: DataContext): Boolean {
if (!super.isAvailable(dataContext)) return false
CommonDataKeys.PROJECT.getData(dataContext) ?: return false
return true
}

override fun buildDialog(project: Project, directory: PsiDirectory, builder: CreateFileFromTemplateDialog.Builder) {
builder.setTitle(CAPTION)
.addKind(SlintBundle.message("list.item.empty.file"), SlintIcons.SLINT, "Slint File")
}

private companion object {
@Nls
private val CAPTION = SlintBundle.message("slint.file")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,14 @@ package dev.slint.ideaplugin.ide.lineMarkers

import com.intellij.execution.lineMarker.RunLineMarkerContributor
import com.intellij.psi.PsiElement
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.TokenType.WHITE_SPACE
import com.intellij.psi.util.elementType
import com.intellij.psi.util.nextLeafs
import dev.slint.ideaplugin.ide.actions.PreviewComponentAction
import dev.slint.ideaplugin.lang.psi.SlintElementTypes.COMPONENT
import dev.slint.ideaplugin.lang.psi.SlintElementTypes.IDENTIFIER
import dev.slint.ideaplugin.lang.psi.SlintFileElementType
import dev.slint.ideaplugin.lang.psi.SlintElementTypes.*

class PreviewRunLineMarkerContributor : RunLineMarkerContributor() {
override fun getInfo(element: PsiElement): Info? {
return when (element.elementType) {
SlintFileElementType -> {
Info(ActionManager.getInstance().getAction("Slint.Preview"))
}
COMPONENT -> {
COMPONENT_DEFINITION -> {
val componentName = getComponentName(element) ?: return null
Info(PreviewComponentAction(componentName))
}
Expand All @@ -29,16 +20,10 @@ class PreviewRunLineMarkerContributor : RunLineMarkerContributor() {
}

private fun getComponentName(element: PsiElement): String? {
val psiWhiteSpace = element.nextSibling
if (psiWhiteSpace.elementType != WHITE_SPACE) {
val componentName = element.children.firstOrNull() ?: return null
if (componentName.elementType != COMPONENT_NAME) {
return null
}

val component = psiWhiteSpace.nextSibling
if (component.elementType != IDENTIFIER) {
return null
}

return component.text
return componentName.text
}
}
6 changes: 6 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<lang.parserDefinition language="Slint"
implementationClass="dev.slint.ideaplugin.lang.parser.SlintParserDefinition"/>
<annotator language="Slint" implementationClass="dev.slint.ideaplugin.ide.annotator.SlintAnnotator"/>
<internalFileTemplate name="Slint File"/>

<!--Syntax Highlighting-->
<lang.syntaxHighlighter language="Slint"
Expand All @@ -47,6 +48,11 @@
implementationClass="dev.slint.ideaplugin.ide.lineMarkers.PreviewRunLineMarkerContributor"/>
</extensions>
<actions>
<action id="Slint.NewSlintFile"
class="dev.slint.ideaplugin.ide.actions.SlintCreateFileAction">
<add-to-group group-id="NewGroup" anchor="after" relative-to-action="NewFile"/>
</action>

<group id="SlintGroupedActions">
<separator/>
<add-to-group group-id="ProjectViewPopupMenu"/>
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
Empty Slint file.
</body>
</html>
7 changes: 6 additions & 1 deletion src/main/resources/messages/SlintBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ settings.display.name=Slint
configurable.name.slint.settings=Slint Settings

# Filetype
filetype.slint.description=Slint
filetype.slint.description=Slint

# Actions
slint.file=Slint File

list.item.empty.file=Empty file

0 comments on commit 5fe1550

Please sign in to comment.