├── .gitignore ├── README.md ├── build.gradle ├── distributions └── 42-header-plugin-1.1.zip ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── resources └── META-INF │ └── plugin.xml ├── settings.gradle └── src └── main ├── java └── fr │ └── eywek │ └── header │ ├── actions │ ├── AddAction.java │ ├── GenerateAction.java │ └── SaveAction.java │ ├── services │ ├── CheckerService.java │ ├── GeneratorService.java │ └── InputService.java │ └── settings │ ├── AppSettingsComponent.java │ ├── AppSettingsConfigurable.java │ └── AppSettingsState.java └── resources └── META-INF └── plugin.xml /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | 42header.iml 3 | .gradle 4 | build 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Header for 42 school 2 | ## Installation 3 | Plugin built versions can be found in the `distribution` directory. 4 | 5 | ## Usage 6 | Can be triggered from the `Tools` menu or by pressing `ctrl + alt + a`. 7 | 8 | Configuration is available in the settings to change username or mail (by default fulfilled from env variables). 9 | Can also be configured to automatically add header to `.c` and `.h` files. 10 | 11 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'org.jetbrains.intellij' version '1.4.0' 3 | id 'java' 4 | } 5 | 6 | group 'fr.eywek.header' 7 | version '1.1' 8 | 9 | repositories { 10 | mavenCentral() 11 | } 12 | 13 | dependencies { 14 | testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2' 15 | testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2' 16 | } 17 | 18 | // See https://github.com/JetBrains/gradle-intellij-plugin/ 19 | intellij { 20 | version = '2021.3.2' 21 | type = 'CL' 22 | } 23 | 24 | patchPluginXml { 25 | } 26 | 27 | test { 28 | useJUnitPlatform() 29 | } -------------------------------------------------------------------------------- /distributions/42-header-plugin-1.1.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Eywek/42-header-plugin/6c72f2309dde104a6548ef49e1c347d4c6848240/distributions/42-header-plugin-1.1.zip -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Eywek/42-header-plugin/6c72f2309dde104a6548ef49e1c347d4c6848240/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MSYS* | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /resources/META-INF/plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | fr.eywek.header 3 | 42Header 4 | 1.0 5 | Eywek 6 | 7 | 10 | 11 | 13 | 14 | 15 | 16 | 17 | com.intellij.modules.lang 18 | 19 | 20 | 21 | 22 | 23 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | fr.eywek.header.Save 33 | 34 | 35 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = '42-header-plugin' 2 | 3 | -------------------------------------------------------------------------------- /src/main/java/fr/eywek/header/actions/AddAction.java: -------------------------------------------------------------------------------- 1 | package fr.eywek.header.actions; 2 | 3 | import com.intellij.openapi.command.WriteCommandAction; 4 | import com.intellij.openapi.components.ApplicationComponent; 5 | import com.intellij.openapi.fileEditor.FileDocumentManager; 6 | import com.intellij.openapi.project.ProjectManager; 7 | import com.intellij.openapi.vfs.VirtualFile; 8 | import com.intellij.openapi.vfs.VirtualFileEvent; 9 | import com.intellij.openapi.vfs.VirtualFileListener; 10 | import com.intellij.openapi.vfs.VirtualFileManager; 11 | import fr.eywek.header.services.CheckerService; 12 | import fr.eywek.header.services.GeneratorService; 13 | import fr.eywek.header.settings.AppSettingsState; 14 | import org.jetbrains.annotations.NotNull; 15 | 16 | import java.util.Objects; 17 | 18 | public class AddAction implements ApplicationComponent { 19 | 20 | protected AppSettingsState state; 21 | protected GeneratorService generatorService; 22 | protected CheckerService checkerService; 23 | 24 | public AddAction() 25 | { 26 | state = AppSettingsState.getInstance().getState(); 27 | generatorService = new GeneratorService(); 28 | checkerService = new CheckerService(); 29 | } 30 | 31 | @Override 32 | public void initComponent() { 33 | VirtualFileManager.getInstance().addVirtualFileListener(new VirtualFileListener() { 34 | @Override 35 | public void fileCreated(@NotNull VirtualFileEvent event) { 36 | VirtualFile file = event.getFile(); 37 | 38 | if (!checkerService.checkIsRightFileType(file)) return; 39 | if (!state.automaticAdd) return; 40 | 41 | String filename = file.getName(); 42 | String username = state.username; 43 | String mail = state.mail; 44 | 45 | String header = generatorService.generateHeader(filename, username, mail); 46 | if (header == null) return; 47 | 48 | Runnable runnable = () -> Objects.requireNonNull(FileDocumentManager.getInstance().getDocument(file)).insertString(0, header); 49 | WriteCommandAction.runWriteCommandAction(ProjectManager.getInstance().getOpenProjects()[0], runnable); 50 | } 51 | }); 52 | } 53 | 54 | @Override 55 | public void disposeComponent() { 56 | } 57 | 58 | @Override 59 | @NotNull 60 | public String getComponentName() { 61 | return "Save"; 62 | } 63 | } -------------------------------------------------------------------------------- /src/main/java/fr/eywek/header/actions/GenerateAction.java: -------------------------------------------------------------------------------- 1 | package fr.eywek.header.actions; 2 | 3 | import com.intellij.openapi.actionSystem.AnAction; 4 | import com.intellij.openapi.actionSystem.AnActionEvent; 5 | import com.intellij.openapi.actionSystem.LangDataKeys; 6 | import com.intellij.openapi.command.WriteCommandAction; 7 | import com.intellij.openapi.vfs.VirtualFile; 8 | import fr.eywek.header.services.CheckerService; 9 | import fr.eywek.header.services.GeneratorService; 10 | import fr.eywek.header.services.InputService; 11 | import fr.eywek.header.settings.AppSettingsState; 12 | 13 | import static com.intellij.openapi.actionSystem.CommonDataKeys.VIRTUAL_FILE; 14 | 15 | public class GenerateAction extends AnAction 16 | { 17 | protected AppSettingsState state; 18 | protected GeneratorService generatorService; 19 | protected CheckerService checkerService; 20 | protected InputService inputService; 21 | 22 | public GenerateAction() 23 | { 24 | state = AppSettingsState.getInstance().getState(); 25 | generatorService = new GeneratorService(); 26 | checkerService = new CheckerService(); 27 | inputService = new InputService(); 28 | } 29 | 30 | @Override 31 | public void actionPerformed(AnActionEvent AnActionEvent) 32 | { 33 | VirtualFile file = AnActionEvent.getData(VIRTUAL_FILE); 34 | 35 | if (!this.checkerService.checkIsRightFileType(file)) return; 36 | if (this.checkerService.checkIfHasHeader(file)) return; 37 | 38 | if (!this.checkerService.checkIfHasUsername(state)) 39 | inputService.setUsernameFromInput(AnActionEvent.getProject()); 40 | 41 | String filename = file.getName(); 42 | String username = state.username; 43 | String mail = state.mail; 44 | 45 | String header = this.generatorService.generateHeader(filename, username, mail); 46 | if (header == null) return; 47 | 48 | Runnable runnable = new Runnable() 49 | { 50 | @Override 51 | public void run() 52 | { 53 | AnActionEvent.getData(LangDataKeys.EDITOR).getDocument().insertString(0, header); 54 | } 55 | }; 56 | WriteCommandAction.runWriteCommandAction(getEventProject(AnActionEvent), runnable); 57 | } 58 | } -------------------------------------------------------------------------------- /src/main/java/fr/eywek/header/actions/SaveAction.java: -------------------------------------------------------------------------------- 1 | package fr.eywek.header.actions; 2 | 3 | import com.intellij.openapi.command.WriteCommandAction; 4 | import com.intellij.openapi.components.ApplicationComponent; 5 | import com.intellij.openapi.fileEditor.FileDocumentManager; 6 | import com.intellij.openapi.project.ProjectManager; 7 | import com.intellij.openapi.vfs.VirtualFile; 8 | import com.intellij.openapi.vfs.VirtualFileEvent; 9 | import com.intellij.openapi.vfs.VirtualFileListener; 10 | import com.intellij.openapi.vfs.VirtualFileManager; 11 | import fr.eywek.header.services.CheckerService; 12 | import fr.eywek.header.services.GeneratorService; 13 | import fr.eywek.header.settings.AppSettingsState; 14 | import org.jetbrains.annotations.NotNull; 15 | 16 | import java.util.Objects; 17 | 18 | public class SaveAction implements ApplicationComponent { 19 | 20 | protected AppSettingsState state; 21 | protected GeneratorService generatorService; 22 | protected CheckerService checkerService; 23 | 24 | public SaveAction() 25 | { 26 | state = AppSettingsState.getInstance().getState(); 27 | generatorService = new GeneratorService(); 28 | checkerService = new CheckerService(); 29 | } 30 | 31 | @Override 32 | public void initComponent() { 33 | VirtualFileManager.getInstance().addVirtualFileListener(new VirtualFileListener() { 34 | public void contentsChanged(@NotNull VirtualFileEvent event) { 35 | VirtualFile file = event.getFile(); 36 | 37 | String username = state.username; 38 | 39 | if (!checkerService.checkIsRightFileType(file)) return; 40 | if (!checkerService.checkIfHasHeader(file)) return; 41 | 42 | String updatedLine = generatorService.updateLineUpdated(file, username); 43 | if (updatedLine == null) return; 44 | 45 | Runnable updatedRunnable = () -> Objects.requireNonNull(FileDocumentManager.getInstance().getDocument(file)).replaceString(648, 648 + updatedLine.length(), updatedLine); 46 | WriteCommandAction.runWriteCommandAction(ProjectManager.getInstance().getOpenProjects()[0], updatedRunnable); 47 | 48 | String filenameLine = generatorService.updateLineFilename(file); 49 | if (filenameLine == null) return; 50 | 51 | Runnable filenameRunnable = () -> Objects.requireNonNull(FileDocumentManager.getInstance().getDocument(file)).replaceString(243, 243 + filenameLine.length(), filenameLine); 52 | WriteCommandAction.runWriteCommandAction(ProjectManager.getInstance().getOpenProjects()[0], filenameRunnable); 53 | } 54 | }); 55 | } 56 | 57 | @Override 58 | public void disposeComponent() { 59 | } 60 | 61 | @Override 62 | @NotNull 63 | public String getComponentName() { 64 | return "Save"; 65 | } 66 | } -------------------------------------------------------------------------------- /src/main/java/fr/eywek/header/services/CheckerService.java: -------------------------------------------------------------------------------- 1 | package fr.eywek.header.services; 2 | 3 | import com.intellij.openapi.editor.Document; 4 | import com.intellij.openapi.fileEditor.FileDocumentManager; 5 | import com.intellij.openapi.util.TextRange; 6 | import com.intellij.openapi.vfs.VirtualFile; 7 | import fr.eywek.header.settings.AppSettingsState; 8 | 9 | public class CheckerService { 10 | 11 | public boolean checkIsRightFileType(VirtualFile file) 12 | { 13 | String filename = file.getName(); 14 | String extension = file.getExtension(); 15 | if (extension == null && !filename.contains("Makefile")) 16 | return (false); 17 | if (extension == null) return (false); 18 | return filename.contains("Makefile") || extension.equals("c") || extension.equals("h"); 19 | } 20 | 21 | public boolean checkIfHasHeader(VirtualFile file) 22 | { 23 | Document document = FileDocumentManager.getInstance().getDocument(file); 24 | if (document == null) return (false); 25 | if (document.getText().length() < 5) return (false); 26 | 27 | String start = document.getText(new TextRange(0, 5)); 28 | return start.equals("/* **") || start.equals("# ***"); 29 | } 30 | 31 | public boolean checkIfHasUsername(AppSettingsState state) 32 | { 33 | return !state.username.isEmpty() && !state.username.isBlank() && state.username != null; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/fr/eywek/header/services/GeneratorService.java: -------------------------------------------------------------------------------- 1 | package fr.eywek.header.services; 2 | 3 | import com.intellij.openapi.fileEditor.FileDocumentManager; 4 | import com.intellij.openapi.util.TextRange; 5 | import com.intellij.openapi.vfs.VirtualFile; 6 | import fr.eywek.header.settings.AppSettingsState; 7 | 8 | import java.text.DateFormat; 9 | import java.text.SimpleDateFormat; 10 | import java.util.Date; 11 | 12 | import static com.intellij.openapi.actionSystem.CommonDataKeys.VIRTUAL_FILE; 13 | 14 | public class GeneratorService { 15 | 16 | protected String getStartComment(String filename) 17 | { 18 | return (filename.contains("Makefile") ? "#" : "/*"); 19 | } 20 | 21 | protected String getEndComment(String filename) 22 | { 23 | return (filename.contains("Makefile") ? "#" : "*/"); 24 | } 25 | 26 | public String generateHeader(String filename, String username, String mail) 27 | { 28 | DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 29 | Date date = new Date(); 30 | StringBuilder filenameBuilder = new StringBuilder(filename); 31 | while (filenameBuilder.length() < 51) 32 | filenameBuilder.append(' '); 33 | filename = filenameBuilder.toString(); 34 | StringBuilder user = new StringBuilder("By: " + username + " " + "<" + mail + ">"); 35 | while (user.length() < 47) 36 | user.append(' '); 37 | StringBuilder user2 = new StringBuilder("by " + username); 38 | while (user2.length() < 21) 39 | user2.append(' '); 40 | StringBuilder user3 = new StringBuilder("by " + username); 41 | while (user3.length() < 20) 42 | user3.append(' '); 43 | String startComment = this.getStartComment(filename); 44 | String endComment = this.getEndComment(filename); 45 | 46 | return (startComment + " " + (startComment.length() == 1 ? "*" : "") + "**************************************************************************" + (endComment.length() == 1 ? "*" : "") + " " + endComment + "\n" + 47 | startComment + (startComment.length() == 1 ? " " : "") + " " + (endComment.length() == 1 ? " ": "") + endComment + "\n" + 48 | startComment + (startComment.length() == 1 ? " " : "") + " ::: :::::::: " + (endComment.length() == 1 ? " ": "") + endComment + "\n" + 49 | startComment + (startComment.length() == 1 ? " " : "") + " " + filename + ":+: :+: :+: " + (endComment.length() == 1 ? " ": "") + endComment + "\n" + 50 | startComment + (startComment.length() == 1 ? " " : "") + " +:+ +:+ +:+ " + (endComment.length() == 1 ? " ": "") + endComment + "\n" + 51 | startComment + (startComment.length() == 1 ? " " : "") + " " + user + "+#+ +:+ +#+ " + (endComment.length() == 1 ? " ": "") + endComment + "\n" + 52 | startComment + (startComment.length() == 1 ? " " : "") + " +#+#+#+#+#+ +#+ " + (endComment.length() == 1 ? ' ': "") + endComment + "\n" + 53 | startComment + (startComment.length() == 1 ? " " : "") + " Created: " + dateFormat.format(date) + " " + user2 + "#+# #+# " + (endComment.length() == 1 ? " ": "") + endComment + "\n" + 54 | startComment + (startComment.length() == 1 ? " " : "") + " Updated: " + dateFormat.format(date) + " " + user3 + "### ########.fr " + (endComment.length() == 1 ? " ": "") + endComment + "\n" + 55 | startComment + (startComment.length() == 1 ? ' ' : "") + " " + (endComment.length() == 1 ? " ": "") + endComment + "\n" + 56 | startComment + " " + (startComment.length() == 1 ? "*" : "") + "**************************************************************************" + (endComment.length() == 1 ? "*" : "") + " " + endComment + "\n"); 57 | } 58 | 59 | public String updateLineUpdated(VirtualFile file, String username) 60 | { 61 | StringBuilder filename = new StringBuilder(file.getName()); 62 | 63 | DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 64 | Date date = new Date(); 65 | while (filename.length() < 51) 66 | filename.append(' '); 67 | StringBuilder user = new StringBuilder("by " + username); 68 | while (user.length() < 20) 69 | user.append(' '); 70 | String header; 71 | if (filename.toString().contains("Makefile")) 72 | header = "# Updated: " + dateFormat.format(date) + " " + user + "### ########.fr #\n"; 73 | else 74 | header = "/* Updated: " + dateFormat.format(date) + " " + user + "### ########.fr */\n"; 75 | 76 | return (header); 77 | } 78 | 79 | public String updateLineFilename(VirtualFile file) 80 | { 81 | StringBuilder filename = new StringBuilder(file.getName()); 82 | String startComment = this.getStartComment(filename.toString()); 83 | String endComment = this.getEndComment(filename.toString()); 84 | 85 | while (filename.length() < 51) 86 | filename.append(' '); 87 | 88 | return (startComment + (startComment.length() == 1 ? " " : "") + " " + filename + ":+: :+: :+: " + (endComment.length() == 1 ? " ": "") + endComment + "\n"); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/main/java/fr/eywek/header/services/InputService.java: -------------------------------------------------------------------------------- 1 | package fr.eywek.header.services; 2 | 3 | import com.intellij.openapi.project.Project; 4 | import com.intellij.openapi.ui.Messages; 5 | import com.intellij.openapi.wm.impl.status.widget.StatusBarWidgetWrapper; 6 | import fr.eywek.header.settings.AppSettingsComponent; 7 | import fr.eywek.header.settings.AppSettingsState; 8 | 9 | import javax.swing.*; 10 | 11 | public class InputService { 12 | private AppSettingsState appSettingsState; 13 | 14 | public InputService() 15 | { 16 | appSettingsState = AppSettingsState.getInstance(); 17 | } 18 | public void setUsernameFromInput(Project project) 19 | { 20 | String message = "No username, please provide one:"; 21 | String title = "username"; 22 | Icon icon = Messages.getQuestionIcon(); 23 | 24 | String inputDialog = Messages.showInputDialog(project, message, title, icon); 25 | appSettingsState.setUsername(inputDialog); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/fr/eywek/header/settings/AppSettingsComponent.java: -------------------------------------------------------------------------------- 1 | package fr.eywek.header.settings; 2 | 3 | import com.intellij.ui.components.JBCheckBox; 4 | import com.intellij.ui.components.JBLabel; 5 | import com.intellij.ui.components.JBTextField; 6 | import com.intellij.util.ui.FormBuilder; 7 | import org.jetbrains.annotations.NotNull; 8 | 9 | import javax.swing.*; 10 | 11 | /** 12 | * Supports creating and managing a {@link JPanel} for the Settings Dialog. 13 | */ 14 | public class AppSettingsComponent { 15 | 16 | private final JPanel mainPanel; 17 | private final JBTextField userNameText = new JBTextField(); 18 | private final JBTextField mailText = new JBTextField(); 19 | private final JCheckBox automaticAddCheckbox = new JBCheckBox(); 20 | 21 | public AppSettingsComponent() { 22 | mainPanel = FormBuilder.createFormBuilder() 23 | .addLabeledComponent(new JBLabel("Username: "), userNameText, 1, false) 24 | .addLabeledComponent(new JBLabel("Mail: "), mailText, 1, false) 25 | .addVerticalGap(3) 26 | .addLabeledComponent(new JBLabel("Automatically add header to .c and .h files"), automaticAddCheckbox, 1, false) 27 | .addComponentFillVertically(new JPanel(), 0) 28 | .getPanel(); 29 | } 30 | 31 | public JPanel getPanel() { 32 | return (mainPanel); 33 | } 34 | 35 | public JComponent getPreferredFocusedComponent() { 36 | return (userNameText); 37 | } 38 | 39 | @NotNull 40 | public String getUserNameText() { 41 | return (userNameText.getText()); 42 | } 43 | 44 | public void setUserNameText(@NotNull String newText) { 45 | userNameText.setText(newText); 46 | } 47 | 48 | @NotNull 49 | public String getMailText() { 50 | return (mailText.getText()); 51 | } 52 | 53 | public void setMailText(@NotNull String newText) { 54 | mailText.setText(newText); 55 | } 56 | 57 | public boolean getAutomaticAddCheckbox() 58 | { 59 | return (automaticAddCheckbox.isSelected()); 60 | } 61 | 62 | public void setAutomaticAddCheckbox(boolean newValue) 63 | { 64 | automaticAddCheckbox.setSelected(newValue); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/fr/eywek/header/settings/AppSettingsConfigurable.java: -------------------------------------------------------------------------------- 1 | package fr.eywek.header.settings; 2 | 3 | import com.intellij.openapi.options.Configurable; 4 | import org.jetbrains.annotations.Nls; 5 | import org.jetbrains.annotations.Nullable; 6 | 7 | import javax.swing.*; 8 | 9 | /** 10 | * Provides controller functionality for application settings. 11 | */ 12 | public class AppSettingsConfigurable implements Configurable { 13 | 14 | private AppSettingsComponent appSettingsComponent; 15 | 16 | @Nls(capitalization = Nls.Capitalization.Title) 17 | @Override 18 | public String getDisplayName() { 19 | return "42 Header"; 20 | } 21 | 22 | @Override 23 | public JComponent getPreferredFocusedComponent() { 24 | return appSettingsComponent.getPreferredFocusedComponent(); 25 | } 26 | 27 | @Nullable 28 | @Override 29 | public JComponent createComponent() { 30 | appSettingsComponent = new AppSettingsComponent(); 31 | return appSettingsComponent.getPanel(); 32 | } 33 | 34 | @Override 35 | public boolean isModified() { 36 | AppSettingsState settings = AppSettingsState.getInstance(); 37 | boolean modified = !appSettingsComponent.getUserNameText().equals(settings.username); 38 | modified |= !appSettingsComponent.getMailText().equals(settings.mail); 39 | modified |= !appSettingsComponent.getAutomaticAddCheckbox() == settings.automaticAdd; 40 | return modified; 41 | } 42 | 43 | @Override 44 | public void apply() { 45 | AppSettingsState settings = AppSettingsState.getInstance(); 46 | settings.username = appSettingsComponent.getUserNameText(); 47 | settings.mail = appSettingsComponent.getMailText(); 48 | settings.automaticAdd = appSettingsComponent.getAutomaticAddCheckbox(); 49 | } 50 | 51 | @Override 52 | public void reset() { 53 | AppSettingsState settings = AppSettingsState.getInstance(); 54 | appSettingsComponent.setUserNameText(settings.username); 55 | appSettingsComponent.setMailText(settings.mail); 56 | appSettingsComponent.setAutomaticAddCheckbox(settings.automaticAdd); 57 | } 58 | 59 | @Override 60 | public void disposeUIResources() { 61 | appSettingsComponent = null; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/fr/eywek/header/settings/AppSettingsState.java: -------------------------------------------------------------------------------- 1 | package fr.eywek.header.settings; 2 | 3 | import com.intellij.openapi.application.ApplicationManager; 4 | import com.intellij.openapi.components.PersistentStateComponent; 5 | import com.intellij.openapi.components.State; 6 | import com.intellij.openapi.components.Storage; 7 | import com.intellij.util.xmlb.XmlSerializerUtil; 8 | import org.jetbrains.annotations.NotNull; 9 | import org.jetbrains.annotations.Nullable; 10 | 11 | /** 12 | * Supports storing the application settings in a persistent way. 13 | * The {@link State} and {@link Storage} annotations define the name of the data and the file name where 14 | * these persistent application settings are stored. 15 | */ 16 | @State( 17 | name = "fr.eywek.header.settings.AppSettingsState", 18 | storages = @Storage("SdkSettingsPlugin.xml") 19 | ) 20 | public class AppSettingsState implements PersistentStateComponent { 21 | 22 | public String username; 23 | public String mail; 24 | public boolean automaticAdd; 25 | 26 | AppSettingsState() 27 | { 28 | username = System.getenv("USER"); 29 | if (username.isEmpty()) username = "marvin"; 30 | 31 | mail = System.getenv("MAIL"); 32 | if (mail.isEmpty()) mail = username + "@student.42.fr"; 33 | 34 | automaticAdd = false; 35 | } 36 | 37 | public static AppSettingsState getInstance() { 38 | return ApplicationManager.getApplication().getService(AppSettingsState.class); 39 | } 40 | 41 | @Nullable 42 | @Override 43 | public AppSettingsState getState() { 44 | return this; 45 | } 46 | 47 | @Override 48 | public void loadState(@NotNull AppSettingsState state) { 49 | XmlSerializerUtil.copyBean(state, this); 50 | } 51 | 52 | public void setUsername(String username) { 53 | this.username = username; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | fr.eywek.header 3 | 42Header 4 | 1.1 5 | Eywek 6 | 7 | 9 | Can be triggered from the `Tools` menu or by pressing `ctrl + alt + a`. 10 | 11 | Configuration is available in the settings to change username or mail (by default fulfilled from env variables). 12 | Can also be configured to automatically add header to `.c` and `.h` files. 13 | ]]> 14 | 15 | 17 | 18 | 19 | 20 | 21 | com.intellij.modules.lang 22 | 23 | 24 | 25 | 26 | 27 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 37 | 40 | 41 | 42 | 43 | 44 | fr.eywek.header.actions.SaveAction 45 | 46 | 47 | 48 | fr.eywek.header.actions.AddAction 49 | 50 | 51 | --------------------------------------------------------------------------------