Skip to content

Commit

Permalink
Modify find
Browse files Browse the repository at this point in the history
  • Loading branch information
naotsugu committed Jan 25, 2024
1 parent 2e41374 commit a40156b
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,14 @@
* FoundReset.
* @author Naotsugu Kobayashi
*/
public record FoundReset() implements Found {
public record FoundReset(long from, long to) implements Found {

public FoundReset() {
this(0, Long.MAX_VALUE);
}

public boolean all() {
return from == 0 && to == Long.MAX_VALUE;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
/**
* The FoundRun.
* @param chOffset the offset
* @param cpOffset the code point offset
* @param length the search result string length
* @param right
* @param peripheral
Expand All @@ -27,6 +28,7 @@
*/
public record FoundRun(
long chOffset,
long cpOffset,
int length,
boolean right,
String peripheral,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public interface Match {
*/
int start();

/**
* Get the matched code point offset.
* @return the matched code point offset
*/
int startCp();

/**
* Get the match result string length
* @return the match result string length
Expand All @@ -45,14 +51,15 @@ default int end() {
* Create a new match.
* @param start the matched offset
* @param length the match result string length
* @param startCp the matched code point offset
* @return a new match
*/
static Match of(int start, int length) {
static Match of(int start, int length, int startCp) {
if (start < 0 || length <= 0) {
throw new IllegalArgumentException();
}
record MatchRecord(int start, int length) implements Match { }
return new MatchRecord(start, length);
record MatchRecord(int start, int length, int startCp) implements Match { }
return new MatchRecord(start, length, startCp);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ private void fire(String text, OffsetPoint point, boolean right, Match match) {
int peripheralEnd = Math.clamp(match.end() + margin, match.end(), text.length());
var found = new FoundRun(
point.offset() + match.start(),
point.cpOffset() + match.startCp(),
match.length(),
right,
text.substring(peripheralStart, peripheralEnd),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,20 @@ public Match[] match(String text) {
return empty;
}

int findCount = 0;
int foundCount = 0;
for (int i = 0; i < text.length();) {
int ret = text.indexOf(cs, i);
if (ret < 0) {
break;
}
if (buffer.length <= findCount) {
buffer = grow(findCount + 1);
if (buffer.length <= foundCount) {
buffer = grow(foundCount + 1);
}
buffer[findCount++] = Match.of(ret, cs.length());
buffer[foundCount++] = Match.of(ret, cs.length(),
text.codePointCount(ret, ret + cs.length()));
i = ret + cs.length();
}
return (findCount == 0) ? empty : Arrays.copyOf(buffer, findCount);
return (foundCount == 0) ? empty : Arrays.copyOf(buffer, foundCount);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,18 @@
*/
public interface ImePallet {

/**
* Turn on the Ime palette.
* @param point the offset point
*/
void on(OffsetPoint point);

/**
* Turn off the Ime palette.
*/
void off();


boolean enabled();

void composed(TextEdit buffer, List<ImeRun> runs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.mammb.code.editor.model.find.FindSpec;
import com.mammb.code.editor.model.find.Found;
import com.mammb.code.editor.model.find.FoundNone;
import com.mammb.code.editor.model.find.FoundReset;
import com.mammb.code.editor.model.find.FoundRun;
import com.mammb.code.editor.model.text.OffsetPoint;
import com.mammb.code.editor.ui.model.FindHandle;
Expand All @@ -36,6 +37,9 @@ public class FindHandleImpl implements FindHandle {
/** The base point. */
private final OffsetPoint basePoint;

/** The next point. */
private OffsetPoint nextPoint;

/** The found first action. */
private final Consumer<Found> foundFirstAction;

Expand All @@ -53,6 +57,7 @@ public class FindHandleImpl implements FindHandle {
private FindHandleImpl(Find find, OffsetPoint basePoint, boolean findAll, Consumer<Found> foundFirstAction) {
this.find = find;
this.basePoint = basePoint;
this.nextPoint = basePoint;
this.findAll = findAll;
this.foundFirstAction = foundFirstAction;
}
Expand All @@ -73,7 +78,8 @@ public static FindHandle of(Find find, OffsetPoint basePoint, boolean findAll, C

@Override
public void findNext(String string, boolean regexp, boolean forward) {
Found found = findFirst(FindSpec.of(string, forward));
var findSpec = FindSpec.of(string, forward);
Found found = findFirst(findSpec);
foundFirstAction.accept(found);
if (findAll && found instanceof FoundRun) {
find.run(OffsetPoint.zero, FindSpec.allOf(string));
Expand All @@ -84,7 +90,8 @@ public void findNext(String string, boolean regexp, boolean forward) {

@Override
public void findAll(String string, boolean regexp) {
Found found = findFirst(FindSpec.of(string, true));
var findSpec = FindSpec.of(string, true);
Found found = findFirst(findSpec);
foundFirstAction.accept(found);
if (found instanceof FoundRun) {
find.run(OffsetPoint.zero, FindSpec.allOf(string));
Expand All @@ -97,15 +104,24 @@ public void setFindAll(boolean findAll) {
}


private Found findFirst(FindSpec findSpec) {
private Found findFirst(FindSpec spec) {
final var consumer = new FoundFirstConsumer();
try {
find.addListener(consumer);
find.run(basePoint, findSpec);
find.run(nextPoint, spec);
} finally {
find.removeListener(consumer);
}
return consumer.foundFirst;
Found found = consumer.foundFirst;
nextPoint = switch (found) {
case FoundRun run -> spec.forward()
? OffsetPoint.of(run.row(), run.chOffset(), run.cpOffset()).plus(run.text())
: OffsetPoint.of(run.row(), run.chOffset(), run.cpOffset());
case FoundReset reset -> basePoint;
case FoundNone none -> nextPoint;
};

return found;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void accept(Found found) {
switch (found) {
case FoundRun run -> founds.put(run.chOffset(), run);
case FoundNone none -> { }
case FoundReset reset -> clear();
case FoundReset reset -> clear(reset);
}
}

Expand All @@ -96,4 +96,16 @@ public void clear() {
founds.clear();
}

/**
* Clear.
* @param reset the reset
*/
void clear(FoundReset reset) {
if (reset.all()) {
clear();
} else {
founds.subMap(reset.from(), reset.to()).clear();
}
}

}

0 comments on commit a40156b

Please sign in to comment.