Skip to content

Commit

Permalink
Merge branch 'master' into feature/commentary
Browse files Browse the repository at this point in the history
  • Loading branch information
citizenmatt committed Apr 18, 2022
2 parents fd0dc0d + e617fc4 commit 7d51768
Show file tree
Hide file tree
Showing 245 changed files with 6,000 additions and 6,681 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import com.maddyhome.idea.vim.command.OperatorArguments
import com.maddyhome.idea.vim.ex.ExException
import com.maddyhome.idea.vim.handler.VimActionHandler
import com.maddyhome.idea.vim.helper.CommandLineHelper
import com.maddyhome.idea.vim.newapi.IjExecutionContext
import com.maddyhome.idea.vim.newapi.IjVimEditor
import com.maddyhome.idea.vim.newapi.ij
import com.maddyhome.idea.vim.vimscript.model.Script
import com.maddyhome.idea.vim.vimscript.parser.VimscriptParser
Expand All @@ -49,7 +47,7 @@ class InsertRegisterAction : VimActionHandler.SingleExecution() {
val expression = readExpression(editor.ij)
if (expression != null) {
if (expression.isNotEmpty()) {
val expressionValue = VimscriptParser.parseExpression(expression)?.evaluate((editor as IjVimEditor).editor, (context as IjExecutionContext).context, Script(listOf()))
val expressionValue = VimscriptParser.parseExpression(expression)?.evaluate(editor, context, Script(listOf()))
?: throw ExException("E15: Invalid expression: $expression")
val textToStore = expressionValue.toInsertableString()
VimPlugin.getRegister().storeTextSpecial('=', textToStore)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class PlaybackRegisterAction : VimActionHandler.SingleExecution() {
try {
var i = 0
while (i < cmd.count) {
res.set(Executor.executeLastCommand(editor.ij, context.ij))
res.set(Executor.executeLastCommand(editor, context))
if (!res.get()) {
break
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/maddyhome/idea/vim/common/CommandAlias.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

package com.maddyhome.idea.vim.common

import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.editor.Editor
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.api.ExecutionContext
import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.ex.ranges.Ranges
import com.maddyhome.idea.vim.helper.MessageHelper
import org.jetbrains.annotations.NonNls
Expand Down Expand Up @@ -126,5 +126,5 @@ sealed class GoalCommand {
}

interface CommandAliasHandler {
fun execute(command: String, ranges: Ranges, editor: Editor, context: DataContext)
fun execute(command: String, ranges: Ranges, editor: VimEditor, context: ExecutionContext)
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ object VimExtensionRegistrar {
}

private fun initExtension(extensionBean: ExtensionBeanClass, name: String) {
if (Executor.executingVimScript) {
if (Executor.executingVimscript) {
delayedExtensionEnabling += extensionBean
} else {
extensionBean.instance.init()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,17 @@ import com.maddyhome.idea.vim.helper.PsiHelper
import com.maddyhome.idea.vim.helper.StringHelper.parseKeys
import com.maddyhome.idea.vim.key.OperatorFunction
import com.maddyhome.idea.vim.newapi.IjVimEditor
import com.maddyhome.idea.vim.newapi.ij
import com.maddyhome.idea.vim.newapi.vim
import java.util.*

class CommentaryExtension : VimExtension {

companion object {
fun doCommentary(editor: Editor, context: DataContext, range: TextRange, selectionType: SelectionType, resetCaret: Boolean): Boolean {
val mode = getInstance(editor.vim).mode
fun doCommentary(editor: VimEditor, context: ExecutionContext, range: TextRange, selectionType: SelectionType, resetCaret: Boolean): Boolean {
val mode = getInstance(editor).mode
if (mode !== CommandState.Mode.VISUAL) {
editor.selectionModel.setSelection(range.startOffset, range.endOffset)
editor.ij.selectionModel.setSelection(range.startOffset, range.endOffset)
}

return runWriteAction {
Expand All @@ -77,11 +78,11 @@ class CommentaryExtension : VimExtension {
IdeActions.ACTION_COMMENT_BLOCK
}

injector.actionExecutor.executeAction(actionName, context.vim)
injector.actionExecutor.executeAction(actionName, context)
} finally {
// Remove the selection, if we added it
if (mode !== CommandState.Mode.VISUAL) {
editor.selectionModel.removeSelection()
editor.removeSelection()
}

// Put the caret back at the start of the range, as though it was moved by the operator's motion argument.
Expand All @@ -92,7 +93,7 @@ class CommentaryExtension : VimExtension {
// then the caret is now in a bit of a weird place. We can't detect this scenario, so we just have to accept
// the difference
if (resetCaret) {
editor.caretModel.primaryCaret.moveToOffset(range.startOffset)
editor.primaryCaret().moveToOffset(range.startOffset)
}
}
}
Expand Down Expand Up @@ -131,7 +132,7 @@ class CommentaryExtension : VimExtension {

override fun apply(editor: Editor, context: DataContext, selectionType: SelectionType): Boolean {
val range = VimPlugin.getMark().getChangeMarks(editor.vim) ?: return false
return doCommentary(editor, context, range, selectionType, true)
return doCommentary(editor.vim, context.vim, range, selectionType, true)
}
}

Expand Down Expand Up @@ -206,7 +207,7 @@ class CommentaryExtension : VimExtension {
* Used like `:1,3Commentary` or `g/fun/Commentary`
*/
private class CommentaryCommandAliasHandler: CommandAliasHandler {
override fun execute(command:String, ranges: Ranges, editor: Editor, context: DataContext) {
override fun execute(command:String, ranges: Ranges, editor: VimEditor, context: ExecutionContext) {
doCommentary(editor, context, ranges.getTextRange(editor, -1), SelectionType.LINE_WISE, false)
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/com/maddyhome/idea/vim/extension/matchit/Matchit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private data class LanguagePatterns(
val reversedClosings: PatternsTable,
) {
// Helper constructor for languages that don't need reversed patterns.
constructor(openings: PatternsTable, closings: PatternsTable): this(openings, closings, openings, closings)
constructor(openings: PatternsTable, closings: PatternsTable) : this(openings, closings, openings, closings)

operator fun plus(newLanguagePatterns: LanguagePatterns): LanguagePatterns {
return LanguagePatterns(
Expand Down Expand Up @@ -270,8 +270,8 @@ private object FileTypePatterns {

return (
LanguagePatterns("<", ">") +
LanguagePatterns(mapOf(openingTagPattern to htmlSearchPair), mapOf(closingTagPattern to htmlSearchPair))
)
LanguagePatterns(mapOf(openingTagPattern to htmlSearchPair), mapOf(closingTagPattern to htmlSearchPair))
)
}

private fun createRubyPatterns(): LanguagePatterns {
Expand All @@ -291,8 +291,8 @@ private object FileTypePatterns {

return (
LanguagePatterns(blockCommentStart, blockCommentEnd) +
LanguagePatterns(openingKeywords, middleKeywords, endKeyword)
)
LanguagePatterns(openingKeywords, middleKeywords, endKeyword)
)
}

private fun createCPatterns(): LanguagePatterns {
Expand All @@ -304,20 +304,19 @@ private object FileTypePatterns {
// Original patterns: https://github.com/vim/vim/blob/master/runtime/ftplugin/make.vim
return (
LanguagePatterns("\\bdefine\\b", "\\bendef\\b") +
LanguagePatterns("(?<!else )ifn?(?:eq|def)\\b", "\\belse(?:\\s+ifn?(?:eq|def))?\\b", "\\bendif\\b")
)
LanguagePatterns("(?<!else )ifn?(?:eq|def)\\b", "\\belse(?:\\s+ifn?(?:eq|def))?\\b", "\\bendif\\b")
)
}

private fun createCMakePatterns(): LanguagePatterns {
// Original patterns: https://github.com/vim/vim/blob/master/runtime/ftplugin/cmake.vim
return (
LanguagePatterns("\\bif\\b", "\\belse(?:if)?\\b", "\\bendif\\b") +
LanguagePatterns("\\b(?:foreach)|(?:while)\\b", "\\bbreak\\b", "\\b(?:endforeach)|(?:endwhile)\\b") +
LanguagePatterns("\\bmacro\\b", "\\bendmacro\\b") +
LanguagePatterns("\\bfunction\\b", "\\bendfunction\\b")
)
LanguagePatterns("\\b(?:foreach)|(?:while)\\b", "\\bbreak\\b", "\\b(?:endforeach)|(?:endwhile)\\b") +
LanguagePatterns("\\bmacro\\b", "\\bendmacro\\b") +
LanguagePatterns("\\bfunction\\b", "\\bendfunction\\b")
)
}

}

/*
Expand Down Expand Up @@ -410,8 +409,9 @@ private fun findMatchingPair(
caretInClosestMatch = true
foundCloserMatch = true
}
} else if (!caretInClosestMatch && matchStart < closestMatchStart
&& !containsDefaultPairs(currentLineChars.subSequence(offset, matchStart))) {
} else if (!caretInClosestMatch && matchStart < closestMatchStart &&
!containsDefaultPairs(currentLineChars.subSequence(offset, matchStart))
) {
// A default pair after the cursor is preferred over any extended pairs after the cursor.
foundCloserMatch = true
}
Expand Down
27 changes: 14 additions & 13 deletions src/main/java/com/maddyhome/idea/vim/extension/nerdtree/NerdTree.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ import com.intellij.ide.projectView.impl.ProjectViewImpl
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.fileEditor.ex.FileEditorManagerEx
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.project.Project
Expand All @@ -41,6 +39,8 @@ import com.intellij.ui.TreeExpandCollapse
import com.intellij.ui.speedSearch.SpeedSearchSupply
import com.intellij.util.ui.tree.TreeUtil
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.api.ExecutionContext
import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.common.CommandAlias
import com.maddyhome.idea.vim.common.CommandAliasHandler
Expand All @@ -57,6 +57,7 @@ import com.maddyhome.idea.vim.helper.StringHelper
import com.maddyhome.idea.vim.helper.runAfterGotFocus
import com.maddyhome.idea.vim.key.MappingOwner
import com.maddyhome.idea.vim.key.RequiredShortcut
import com.maddyhome.idea.vim.newapi.ij
import com.maddyhome.idea.vim.newapi.vim
import com.maddyhome.idea.vim.vimscript.model.datatypes.VimString
import java.awt.event.KeyEvent
Expand Down Expand Up @@ -144,14 +145,14 @@ class NerdTree : VimExtension {
}

class IjCommandHandler(private val actionId: String) : CommandAliasHandler {
override fun execute(command: String, ranges: Ranges, editor: Editor, context: DataContext) {
override fun execute(command: String, ranges: Ranges, editor: VimEditor, context: ExecutionContext) {
callAction(actionId, context)
}
}

class ToggleHandler : CommandAliasHandler {
override fun execute(command: String, ranges: Ranges, editor: Editor, context: DataContext) {
val project = editor.project ?: return
override fun execute(command: String, ranges: Ranges, editor: VimEditor, context: ExecutionContext) {
val project = editor.ij.project ?: return
val toolWindow = ToolWindowManagerEx.getInstanceEx(project).getToolWindow(ToolWindowId.PROJECT_VIEW) ?: return
if (toolWindow.isVisible) {
toolWindow.hide()
Expand All @@ -162,8 +163,8 @@ class NerdTree : VimExtension {
}

class CloseHandler : CommandAliasHandler {
override fun execute(command: String, ranges: Ranges, editor: Editor, context: DataContext) {
val project = editor.project ?: return
override fun execute(command: String, ranges: Ranges, editor: VimEditor, context: ExecutionContext) {
val project = editor.ij.project ?: return
val toolWindow = ToolWindowManagerEx.getInstanceEx(project).getToolWindow(ToolWindowId.PROJECT_VIEW) ?: return
if (toolWindow.isVisible) {
toolWindow.hide()
Expand Down Expand Up @@ -218,7 +219,7 @@ class NerdTree : VimExtension {

val action = nextNode.actionHolder
when (action) {
is NerdAction.ToIj -> callAction(action.name, e.dataContext)
is NerdAction.ToIj -> callAction(action.name, e.dataContext.vim)
is NerdAction.Code -> e.project?.let { action.action(it, e.dataContext, e) }
}
}
Expand Down Expand Up @@ -354,7 +355,7 @@ class NerdTree : VimExtension {
currentWindow.split(SwingConstants.VERTICAL, true, file, true)

// FIXME: 22.01.2021 This solution bouncing a bit
callAction("ActivateProjectToolWindow", context)
callAction("ActivateProjectToolWindow", context.vim)
}
)
registerCommand(
Expand All @@ -365,7 +366,7 @@ class NerdTree : VimExtension {
val currentWindow = splitters.currentWindow
currentWindow.split(SwingConstants.HORIZONTAL, true, file, true)

callAction("ActivateProjectToolWindow", context)
callAction("ActivateProjectToolWindow", context.vim)
}
)
registerCommand(
Expand Down Expand Up @@ -503,19 +504,19 @@ class NerdTree : VimExtension {

private val LOG = logger<NerdTree>()

fun callAction(name: String, context: DataContext) {
fun callAction(name: String, context: ExecutionContext) {
val action = ActionManager.getInstance().getAction(name) ?: run {
VimPlugin.showMessage(MessageHelper.message("action.not.found.0", name))
return
}
val application = ApplicationManager.getApplication()
if (application.isUnitTestMode) {
injector.actionExecutor.executeAction(action.vim, context.vim)
injector.actionExecutor.executeAction(action.vim, context)
} else {
runAfterGotFocus {
injector.actionExecutor.executeAction(
action.vim,
context.vim,
context,
)
}
}
Expand Down
68 changes: 68 additions & 0 deletions src/main/java/com/maddyhome/idea/vim/group/IjStatisticsService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* IdeaVim - Vim emulator for IDEs based on the IntelliJ platform
* Copyright (C) 2003-2022 The IdeaVim authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/*
* IdeaVim - Vim emulator for IDEs based on the IntelliJ platform
* Copyright (C) 2003-2022 The IdeaVim authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.maddyhome.idea.vim.group

import com.maddyhome.idea.vim.api.VimStatistics
import com.maddyhome.idea.vim.statistic.ActionTracker
import com.maddyhome.idea.vim.statistic.VimscriptState

class IjStatisticsService : VimStatistics {

override fun logTrackedAction(actionId: String) {
ActionTracker.logTrackedAction(actionId)
}

override fun logCopiedAction(actionId: String) {
ActionTracker.logCopiedAction(actionId)
}

override fun setIfLoopUsed(value: Boolean) {
VimscriptState.isLoopUsed = value
}

override fun setIfFunctionCallUsed(value: Boolean) {
VimscriptState.isFunctionCallUsed = value
}

override fun setIfFunctionDeclarationUsed(value: Boolean) {
VimscriptState.isFunctionDeclarationUsed = value
}

override fun setIfIfUsed(value: Boolean) {
VimscriptState.isIfUsed = value
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public boolean processExEntry(final @NotNull Editor editor, final @NotNull DataC

if (logger.isDebugEnabled()) logger.debug("swing=" + SwingUtilities.isEventDispatchThread());

Executor.INSTANCE.execute(text, editor, context, false, true, CommandLineVimLContext.INSTANCE);
Executor.INSTANCE.execute(text, new IjVimEditor(editor), new IjExecutionContext(context), false, true, CommandLineVimLContext.INSTANCE);
}
catch (ExException e) {
VimPlugin.showMessage(e.getMessage());
Expand Down
Loading

0 comments on commit 7d51768

Please sign in to comment.