-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
116 additions
and
51 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/main/kotlin/cc/allape/caddyfile/editor/action/GenerateRandomPasswordIntentionAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cc.allape.caddyfile.editor.action | ||
|
||
import cc.allape.caddyfile.ElementFactory | ||
import com.intellij.codeInsight.intention.IntentionAction | ||
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiFile | ||
import kotlin.random.Random | ||
|
||
const val CharSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-" | ||
|
||
class GenerateRandomPasswordIntentionAction : IntentionAction { | ||
override fun startInWriteAction(): Boolean { | ||
return true | ||
} | ||
|
||
override fun getFamilyName(): String { | ||
return "Caddyfile password generation" | ||
} | ||
|
||
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile?): Boolean { | ||
return getPasswordArg(editor, file) != null | ||
} | ||
|
||
override fun getText(): String { | ||
return "Create a URL-safe random password" | ||
} | ||
|
||
override fun invoke(project: Project, editor: Editor?, file: PsiFile?) { | ||
if (editor == null || file == null) { | ||
return | ||
} | ||
|
||
val ele = getPasswordArg(editor, file) ?: return | ||
val password = ele.text.trim() | ||
|
||
val randomPassword = | ||
(1..password.length).map { Random.nextInt(CharSet.length) }.map(CharSet::get).joinToString("") | ||
|
||
ele.replace(ElementFactory.createArg(project, randomPassword)) | ||
} | ||
|
||
override fun generatePreview(project: Project, editor: Editor, file: PsiFile): IntentionPreviewInfo { | ||
return IntentionPreviewInfo.EMPTY | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/kotlin/cc/allape/caddyfile/editor/action/GetPasswordArg.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package cc.allape.caddyfile.editor.action | ||
|
||
import cc.allape.caddyfile.language.psi.CaddyfileTypes | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiFile | ||
import com.intellij.psi.TokenType | ||
import com.intellij.psi.util.elementType | ||
|
||
fun getPasswordArg(editor: Editor?, file: PsiFile?): PsiElement? { | ||
if (editor == null || file == null) { | ||
return null | ||
} | ||
|
||
var ele = file.findElementAt(editor.caretModel.offset) | ||
|
||
if (ele?.elementType == TokenType.WHITE_SPACE) { | ||
val prevProperty = ele?.prevSibling | ||
if (prevProperty?.elementType == CaddyfileTypes.PROPERTY) { | ||
ele = prevProperty?.lastChild | ||
} | ||
} | ||
|
||
if (ele == null) { | ||
return null | ||
} | ||
|
||
// property: | ||
// basic_auth | ||
// matcher? | ||
// hash_algorithm? | ||
// block: | ||
// property: | ||
// username | ||
// password | ||
|
||
val property = ele.parent | ||
val block = property?.parent | ||
val topProperty = block?.parent | ||
|
||
if ( | ||
ele.elementType != CaddyfileTypes.ARG || | ||
property?.elementType != CaddyfileTypes.PROPERTY || | ||
topProperty?.firstChild?.text != "basic_auth" | ||
) { | ||
return null | ||
} | ||
|
||
return ele | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
9 changes: 9 additions & 0 deletions
9
...in/resources/intentionDescriptions/GenerateRandomPasswordIntentionAction/description.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<html lang="en"> | ||
<body> | ||
Create a URL-safe random password base on the same length of current password. | ||
<pre><code lang="kotlin"> | ||
(1..password.length).map { Random.nextInt(CharSet.length) }.map(CharSet::get).joinToString("") | ||
</code></pre> | ||
<!-- tooltip end --> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,7 @@ a { | |
abc | ||
} | ||
basic_auth { | ||
bob 12345678 | ||
bob O5wTeDsRNyreumNXGEHzQ6QW | ||
} | ||
} | ||
|
||
|