-
Notifications
You must be signed in to change notification settings - Fork 192
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: select text when opening find/replace #1516
Changes from 3 commits
6e4a5b7
cb15a52
c7c7048
d560ee2
672391c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,13 +38,16 @@ const regexes = pairs.map( | |
] as const, | ||
); | ||
|
||
type QuoteType = '"' | '"""'; // Define a type that restricts to either single or triple quotes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if its not too much trouble, could we add some tests to describe("should not modify original code when going in and out", () => {
const cases = [
'mo.md("""# Markdown Title\n\nSome content here.""")',
'mo.md("Some *markdown* content")',
"mo.md('''# Another Title\nContent''')",
'mo.md(f"""This is some \\"""content\\"""!""")',
'mo.md("""This is some \\"""content\\"""!""")',
'mo.md("This is some \\"content\\"!")',
"mo.md('This is some \\'content\\'!')",
'mo.md("""Markdown with an escaped \\"quote\\"""")',
'mo.md("""# Title\n\n```python\nprint("Hello, Markdown!")\n```""")',
'mo.md("")',
'mo.md("# Title without proper delimiters")',
'mo.md(""" \n# Title\nContent\n """)',
];
it.each(cases)("should not modify %s", (pythonCode) => {
const [innerCode] = adapter.transformIn(pythonCode);
const [wrappedCode] = adapter.transformOut(innerCode);
expect(pythonCode).toBe(wrappedCode);
});
}); there is also a case where we cannot keep the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. basically - i don't think we want to downgrade from a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
yeah, this is what i intend to do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
i should've considered these two cases There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
yeah, i'll add more tests |
||
|
||
/** | ||
* Language adapter for Markdown. | ||
*/ | ||
export class MarkdownLanguageAdapter implements LanguageAdapter { | ||
type = "markdown" as const; | ||
|
||
lastQuotePrefix: PrefixKind = ""; | ||
lastQuoteType: QuoteType = '"'; // Use the new QuoteType here | ||
|
||
transformIn(pythonCode: string): [string, number] { | ||
if (!this.isSupported(pythonCode)) { | ||
|
@@ -55,14 +58,12 @@ export class MarkdownLanguageAdapter implements LanguageAdapter { | |
const match = pythonCode.match(regex); | ||
if (match) { | ||
const innerCode = match[1]; | ||
|
||
const [quotePrefix, quoteType] = splitQuotePrefix(start); | ||
// store the quote prefix for later when we transform out | ||
this.lastQuotePrefix = quotePrefix; | ||
this.lastQuoteType = quoteType as QuoteType; // Cast to QuoteType | ||
const unescapedCode = innerCode.replaceAll(`\\${quoteType}`, quoteType); | ||
|
||
const offset = pythonCode.indexOf(innerCode); | ||
// string-dedent expects the first and last line to be empty / contain only whitespace, so we pad with \n | ||
return [dedent(`\n${unescapedCode}\n`).trim(), offset]; | ||
} | ||
} | ||
|
@@ -71,23 +72,26 @@ export class MarkdownLanguageAdapter implements LanguageAdapter { | |
} | ||
|
||
transformOut(code: string): [string, number] { | ||
// Get the quote type from the last transformIn | ||
// const prefix = upgradePrefixKind(this.lastQuotePrefix, code); | ||
const prefix = this.lastQuotePrefix; | ||
const quoteType = this.lastQuoteType; // Already validated as QuoteType | ||
|
||
const isOneLine = !code.includes("\n"); | ||
if (isOneLine) { | ||
const escapedCode = code.replaceAll('"', '\\"'); | ||
const start = `mo.md(${prefix}"`; | ||
const end = `")`; | ||
const escapedCode = code.replaceAll(quoteType, `\\${quoteType}`); | ||
const start = `mo.md(${prefix}${quoteType}`; | ||
const end = `${quoteType})`; | ||
return [start + escapedCode + end, start.length]; | ||
} | ||
|
||
// Multiline code | ||
const start = `mo.md(\n ${prefix}"""\n`; | ||
const multilineStart = `mo.md(\n ${prefix}"""\n`; | ||
const multilineEnd = `\n """\n)`; | ||
|
||
// Multiline code that needs formatting | ||
const escapedCode = code.replaceAll('"""', '\\"""'); | ||
const end = `\n """\n)`; | ||
return [start + indentOneTab(escapedCode) + end, start.length + 1]; | ||
return [ | ||
multilineStart + indentOneTab(escapedCode) + multilineEnd, | ||
multilineStart.length + 1, | ||
]; | ||
} | ||
|
||
isSupported(pythonCode: string): boolean { | ||
|
@@ -159,7 +163,10 @@ function splitQuotePrefix(quote: string): [PrefixKind, string] { | |
); | ||
for (const prefix of prefixKindsByLength) { | ||
if (quote.startsWith(prefix)) { | ||
return [prefix, quote.slice(prefix.length)]; | ||
const remaining = quote.slice(prefix.length); | ||
if (remaining.startsWith('"""') || remaining.startsWith('"')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we want this? i would expect |
||
return [prefix, remaining]; | ||
} | ||
} | ||
} | ||
return ["", quote]; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
awesome! nice touch