mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-16 14:23:28 +07:00
extend python-copyright as a plugin
This commit is contained in:
16
python/python-copyright/intellij.python.copyright.iml
Normal file
16
python/python-copyright/intellij.python.copyright.iml
Normal file
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="module" module-name="intellij.copyright" />
|
||||
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
|
||||
<orderEntry type="module" module-name="intellij.platform.core" />
|
||||
<orderEntry type="module" module-name="intellij.python.community.impl" />
|
||||
</component>
|
||||
</module>
|
||||
12
python/python-copyright/resources/META-INF/plugin.xml
Normal file
12
python/python-copyright/resources/META-INF/plugin.xml
Normal file
@@ -0,0 +1,12 @@
|
||||
<idea-plugin xmlns:xi="http://www.w3.org/2001/XInclude">
|
||||
<name>Python Copyright</name>
|
||||
<id>org.jetbrains.plugins.python-copyright</id>
|
||||
<version>VERSION</version>
|
||||
<vendor>JetBrains</vendor>
|
||||
|
||||
<depends>com.intellij.modules.python</depends>
|
||||
<depends>com.intellij.copyright</depends>
|
||||
|
||||
<xi:include href="/META-INF/python-terminal-plugin.xml" xpointer="xpointer(/idea-plugin/*)"/>
|
||||
|
||||
</idea-plugin>
|
||||
@@ -0,0 +1,5 @@
|
||||
<idea-plugin>
|
||||
<extensions defaultExtensionNs="com.intellij.copyright">
|
||||
<updater filetype="Python" implementationClass="com.jetbrains.python.copyright.PyUpdateCopyrightsProvider"/>
|
||||
</extensions>
|
||||
</idea-plugin>
|
||||
@@ -0,0 +1,83 @@
|
||||
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.jetbrains.python.copyright
|
||||
|
||||
import com.intellij.openapi.fileTypes.FileType
|
||||
import com.intellij.openapi.module.Module
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.psi.PsiComment
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
import com.jetbrains.python.psi.PyFile
|
||||
import com.maddyhome.idea.copyright.CopyrightProfile
|
||||
import com.maddyhome.idea.copyright.options.LanguageOptions
|
||||
import com.maddyhome.idea.copyright.psi.UpdateCopyright
|
||||
import com.maddyhome.idea.copyright.psi.UpdateCopyrightsProvider
|
||||
import com.maddyhome.idea.copyright.psi.UpdatePsiFileCopyright
|
||||
import java.util.function.Predicate
|
||||
import java.util.regex.Pattern
|
||||
|
||||
class PyUpdateCopyrightsProvider : UpdateCopyrightsProvider() {
|
||||
override fun createInstance(project: Project,
|
||||
module: Module?,
|
||||
file: VirtualFile,
|
||||
base: FileType?,
|
||||
options: CopyrightProfile?): UpdateCopyright = PyUpdateFileCopyright(project, module, file, options)
|
||||
|
||||
override fun getDefaultOptions(): LanguageOptions {
|
||||
val options = super.getDefaultOptions()
|
||||
options.block = false
|
||||
options.fileTypeOverride = LanguageOptions.USE_TEXT
|
||||
return options
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class PyUpdateFileCopyright(project: Project, module: Module?, root: VirtualFile, options: CopyrightProfile?) : UpdatePsiFileCopyright(
|
||||
project, module, root, options) {
|
||||
|
||||
override fun accept() = file is PyFile
|
||||
|
||||
override fun scanFile() {
|
||||
val f = file as PyFile
|
||||
|
||||
val first = if (f.firstChild != null) first(f) else f
|
||||
|
||||
for (block in arrayOf(f.importBlock, f.topLevelClasses, f.topLevelAttributes, f.topLevelFunctions)) {
|
||||
if (block.size > 0) {
|
||||
checkComments(first, block.first(), true)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
checkComments(first, f, true)
|
||||
}
|
||||
}
|
||||
|
||||
private fun first(file: PyFile): PsiElement = coding(shebang(file.firstChild))
|
||||
|
||||
private fun isShebangComment(element: PsiElement) = element is PsiComment && element.text.startsWith("#!")
|
||||
|
||||
private fun shebang(element: PsiElement) = if (isShebangComment(element)) {
|
||||
skipWhitespaceForwardNotNull(element.nextSibling)
|
||||
}
|
||||
else {
|
||||
element
|
||||
}
|
||||
|
||||
private fun skipWhitespaceForwardNotNull(e: PsiElement): PsiElement {
|
||||
return PsiTreeUtil.skipWhitespacesForward(e) ?: e
|
||||
}
|
||||
|
||||
private val ENCODING_PATTERN: Predicate<String> = Pattern.compile("^[ \\t\\f]*#.*?coding[:=][ \\t]*([-_.a-zA-Z0-9]+)").asPredicate()
|
||||
|
||||
|
||||
private fun isEncodingComment(element: PsiElement) = element is PsiComment && ENCODING_PATTERN.test(element.text)
|
||||
|
||||
private fun coding(element: PsiElement) = if (isEncodingComment(element)) {
|
||||
skipWhitespaceForwardNotNull(element.nextSibling)
|
||||
}
|
||||
else {
|
||||
element
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user