Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add emacs tab support for makefile #801

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions makefile/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Plugin source code was previously located at <a href="https://github.com/kropp/i
<runConfigurationProducer implementation="com.jetbrains.lang.makefile.MakefileRunConfigurationProducer"/>
<codeStyleSettingsProvider implementation="com.jetbrains.lang.makefile.MakefileCodeStyleSettingsProvider"/>
<langCodeStyleSettingsProvider implementation="com.jetbrains.lang.makefile.MakefileLangCodeStyleSettingsProvider"/>
<lang.formatter language="Makefile" implementationClass="com.jetbrains.lang.makefile.MakefileFormattingModelBuilder" />
<lang.emacs language="Makefile" implementationClass="com.jetbrains.lang.makefile.MakefileEmacsHandler" />
<lang.elementManipulator implementationClass="com.jetbrains.lang.makefile.psi.MakefilePrerequisiteManipulator"
forClass="com.jetbrains.lang.makefile.psi.impl.MakefilePrerequisiteImpl"/>
<lang.elementManipulator implementationClass="com.jetbrains.lang.makefile.psi.MakefileVariableUsageManipulator"
Expand Down
24 changes: 24 additions & 0 deletions makefile/src/com/jetbrains/lang/makefile/MakefileEmacsHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.jetbrains.lang.makefile

import com.intellij.codeInsight.editorActions.emacs.EmacsProcessingHandler
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile
import com.intellij.util.DocumentUtil

class MakefileEmacsHandler : EmacsProcessingHandler {
override fun changeIndent(project: Project, editor: Editor, file: PsiFile): EmacsProcessingHandler.Result {
val selectionModel = editor.selectionModel
if (selectionModel.hasSelection()) {
return EmacsProcessingHandler.Result.CONTINUE
}

// make-mode.el just inserts a tab character.
// auto indentation is left for a to-do.
val caretOffset: Int = editor.caretModel.offset
editor.document.insertString(caretOffset, "\t")
editor.caretModel.moveToOffset(caretOffset + 1)

return EmacsProcessingHandler.Result.STOP
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.jetbrains.lang.makefile

import com.intellij.formatting.*
import com.intellij.lang.ASTNode
import com.intellij.psi.codeStyle.CodeStyleSettings
import com.intellij.psi.formatter.common.AbstractBlock
import com.intellij.psi.formatter.common.SettingsAwareBlock
import com.jetbrains.lang.makefile.psi.MakefileTypes

internal class MakefileFormattingBlock(
node: ASTNode,
private val settings: CodeStyleSettings, protected val spacing: SpacingBuilder,
alignment: Alignment? = null, wrap: Wrap? = null
) : AbstractBlock(node, wrap, alignment), SettingsAwareBlock {
override fun getSettings(): CodeStyleSettings = settings

override fun isLeaf(): Boolean = subBlocks.isEmpty()

override fun getSpacing(child1: Block?, child2: Block): Spacing? = spacing.getSpacing(this, child1, child2)

override fun getIndent(): Indent? {
return Indent.getNormalIndent();
}

override fun buildChildren(): List<Block> {
val newAlignment = Alignment.createAlignment()

return this.children(node)
.filter { it.elementType != MakefileTypes.TAB }
.map { MakefileFormattingBlock(it, settings, spacing, newAlignment) }
.toList()
}

private fun children(node: ASTNode) = sequence<ASTNode> {
var child = node.firstChildNode;
while(child != null) {
yield(child)
child = child.treeNext
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.jetbrains.lang.makefile

import com.intellij.formatting.*
import com.intellij.psi.formatter.DocumentBasedFormattingModel

class MakefileFormattingModelBuilder : FormattingModelBuilder {
override fun createModel(formattingContext: FormattingContext): FormattingModel {
val settings = formattingContext.codeStyleSettings
val block = MakefileFormattingBlock(formattingContext.node, settings, SpacingBuilder(settings, MakefileLanguage))

return DocumentBasedFormattingModel(block, settings, formattingContext.containingFile)
}
}