mirror of
https://github.com/chylex/IntelliJ-IdeaVim.git
synced 2025-04-16 02:15:43 +02:00
Added to support pattern ranges for Ex commands
This commit is contained in:
parent
aebd17e18a
commit
f802aa1722
159
src/com/maddyhome/idea/vim/ex/range/SearchRange.java
Normal file
159
src/com/maddyhome/idea/vim/ex/range/SearchRange.java
Normal file
@ -0,0 +1,159 @@
|
||||
package com.maddyhome.idea.vim.ex.range;
|
||||
|
||||
/*
|
||||
* IdeaVim - A Vim emulator plugin for IntelliJ Idea
|
||||
* Copyright (C) 2003 Rick Maddy
|
||||
*
|
||||
* 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, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
import com.intellij.openapi.actionSystem.DataContext;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.maddyhome.idea.vim.command.Command;
|
||||
import com.maddyhome.idea.vim.group.CommandGroups;
|
||||
import com.maddyhome.idea.vim.helper.EditorHelper;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Represents a range given by a search pattern. The pattern can be '\\/', '\\?', '\\&', /{pattern}/,
|
||||
* or ?{pattern}?. The last two can be repeated 0 or more times after any of the others.
|
||||
*/
|
||||
public class SearchRange extends AbstractRange
|
||||
{
|
||||
/**
|
||||
* Create the pattern range
|
||||
* @param pattern The text of the pattern. Each subpattern must be separated by the nul character (\\u0000)
|
||||
* @param offset The range offset
|
||||
* @param move True if the cursor should be moved
|
||||
*/
|
||||
public SearchRange(String pattern, int offset, boolean move)
|
||||
{
|
||||
super(offset, move);
|
||||
setPattern(pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the pattern into a list of subpatterns and flags
|
||||
* @param pattern The full search pattern
|
||||
*/
|
||||
private void setPattern(String pattern)
|
||||
{
|
||||
logger.debug("pattern=" + pattern);
|
||||
StringTokenizer tok = new StringTokenizer(pattern, "\u0000");
|
||||
while (tok.hasMoreTokens())
|
||||
{
|
||||
String pat = tok.nextToken();
|
||||
if (pat.equals("\\/"))
|
||||
{
|
||||
patterns.add(CommandGroups.getInstance().getSearch().getLastSearch());
|
||||
flags.add(new Integer(Command.FLAG_SEARCH_FWD));
|
||||
}
|
||||
else if (pat.equals("\\?"))
|
||||
{
|
||||
patterns.add(CommandGroups.getInstance().getSearch().getLastSearch());
|
||||
flags.add(new Integer(Command.FLAG_SEARCH_REV));
|
||||
}
|
||||
else if (pat.equals("\\&"))
|
||||
{
|
||||
patterns.add(CommandGroups.getInstance().getSearch().getLastPattern());
|
||||
flags.add(new Integer(Command.FLAG_SEARCH_FWD));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (pat.charAt(0) == '/')
|
||||
{
|
||||
flags.add(new Integer(Command.FLAG_SEARCH_FWD));
|
||||
}
|
||||
else
|
||||
{
|
||||
flags.add(new Integer(Command.FLAG_SEARCH_REV));
|
||||
}
|
||||
|
||||
pat = pat.substring(1);
|
||||
if (pat.charAt(pat.length() - 1) == pat.charAt(0))
|
||||
{
|
||||
pat = pat.substring(0, pat.length() - 1);
|
||||
}
|
||||
patterns.add(pat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the line number specified by this range without regard to any offset.
|
||||
* @param editor The editor to get the line for
|
||||
* @param context The data context
|
||||
* @param lastZero True if last line was set to start of file
|
||||
* @return The zero based line number, -1 if the text was not found
|
||||
*/
|
||||
protected int getRangeLine(Editor editor, DataContext context, boolean lastZero)
|
||||
{
|
||||
// Each subsequent pattern is searched for starting in the line after the previous search match
|
||||
int line = EditorHelper.getCurrentLogicalLine(editor);
|
||||
int pos = -1;
|
||||
for (int i = 0; i < patterns.size(); i++)
|
||||
{
|
||||
String pattern = (String)patterns.get(i);
|
||||
int flag = ((Integer)flags.get(i)).intValue();
|
||||
if ((flag & Command.FLAG_SEARCH_FWD) != 0 && !lastZero)
|
||||
{
|
||||
pos = CommandGroups.getInstance().getMotion().moveCaretToLineEnd(editor, line, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
pos = CommandGroups.getInstance().getMotion().moveCaretToLineStart(editor, line);
|
||||
}
|
||||
|
||||
pos = CommandGroups.getInstance().getSearch().search(editor, context, pattern, pos, 1, flag);
|
||||
if (pos == -1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
line = editor.offsetToLogicalPosition(pos).line;
|
||||
}
|
||||
}
|
||||
|
||||
if (pos != -1)
|
||||
{
|
||||
return line;
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
StringBuffer res = new StringBuffer();
|
||||
res.append("SearchRange[");
|
||||
res.append("patterns=");
|
||||
res.append(patterns);
|
||||
res.append(", ");
|
||||
res.append(super.toString());
|
||||
res.append("]");
|
||||
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
private ArrayList patterns = new ArrayList();
|
||||
private ArrayList flags = new ArrayList();
|
||||
|
||||
private static Logger logger = Logger.getInstance(SearchRange.class.getName());
|
||||
}
|
Loading…
Reference in New Issue
Block a user