-
-
Notifications
You must be signed in to change notification settings - Fork 321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix passage suggest to allow spaces. Closes #1176 #1581
base: develop
Are you sure you want to change the base?
Conversation
It's been a long time since I've touched this code, but the CodeMirror docs suggest that we can use the export function useCodeMirrorPassageHints(story: Story) {
return React.useCallback(
(editor: Editor) => {
editor.showHint({
closeCharacters: /\]/,
completeSingle: false,
// and the rest of it as-is and this seems to work locally for me. But does your approach handle certain scenarios better? I only tried a basic example, but I did notice in your approach that other characters that are defaulted by Only other thing I wonder about is if we go with |
Hi. My approach already uses Most of my code is not about controlling when the suggestion menu closes, but about making sure that the selection doesn't break on a space due to the use of Just changing If you just change |
OK, I see. How about this as the hint() function then? hint() {
// Get the current cursor position and line content.
const cursor = editor.getCursor();
const line = editor.getLine(cursor.line);
const from = {...cursor};
const to = {...cursor};
// Expand the range to the first `[` before the cursor. lastIndexOf() will
// either give us -1, if there was no match, or the first bracket. In either
// case, we want to add one so that it either points to the start of the line,
// or the first character after the match. e.g. `[passage name` becomes
// `passage name`.
from.ch = line.lastIndexOf('[', from.ch) + 1;
const candidate = line.substring(from.ch, to.ch).toLowerCase();
const comps = {
from,
to,
list: story.passages.reduce<string[]>((result, passage) => {
if (passage.name.toLowerCase().includes(candidate)) {
return [...result, passage.name];
}
return result;
}, [])
};
CodeMirror.on(comps, 'pick', () => {
const doc = editor.getDoc();
doc.replaceRange(']] ', doc.getCursor());
});
return comps;
} It would be nice to not have to take an additional dependency on CodeMirror because it'll complicate the transition to version 6. Also, did you experiment any with unit tests? In trying this out locally, there seem to be many scenarios to watch out for here (typing links at the start, middle, and end of a paragraph) that it would be good to get test coverage on this. |
I did a bunch of tests in various locations in the text, all of which were find, and I also made sure it passed the Jest tests, which I rewrote to mock the extra search methods. I take it the extra dependency you were trying to avoid is the |
Yes--it's a separate library, I think, but that's partly because CM 6 is now a lot more modular. Sorry, by unit tests, I meant Jest tests. it would be good to add tests around the behavior you've added in this PR. I can try to make a pass on that depending on your comfort level. |
I'm not very knowledgable about Jest. I did make some changes to the existing test, extending the mocks and the like, but I'm not sure I would know how to modify it for the new functions |
No worries. I’ll try that out soon and amend the PR. Thanks for getting the ball rolling on this! |
Description
This change makes the passage suggestion menu (when typing a link) work correctly when typing a link or passage name with a space in it.
Issues Fixed
This change resolves issues: #1176 (and similar)
Credit
Please put an X in one of the squares below only.
[X] I would like to be credited in the application as: Hituro (David Donachie)
[ ] I would not like my name to appear in the application credits.
Presubmission Checklist
Put an X in the squares below to indicate you've done each step.
[X] I have read this project's CONTRIBUTING file and this PR follows the criteria laid out there.
[X] This contribution was created by me and I have the right to submit it under the GPL v3 license. (This is not a rights assignment.)
[X] I have read and agree to abide by this project's Code of Conduct.