Skip to content

Commit

Permalink
Refactor some languages
Browse files Browse the repository at this point in the history
  • Loading branch information
FIameCaster committed Dec 27, 2023
1 parent 32dd207 commit 12c1752
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 17 deletions.
4 changes: 2 additions & 2 deletions package/src/languages/css.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { languageMap } from "../core"
import { isBracketPair } from "./patterns"
import { isBracketPair, openBracket } from "./patterns"

languageMap.css =
languageMap.sass =
Expand All @@ -9,7 +9,7 @@ languageMap.css =
block: ["/*", "*/"],
},
autoIndent: [
([start], value) => /[([{][^\n)\]}]*$/.test(value.slice(0, start)),
([start], value) => openBracket.test(value.slice(0, start)),
([start, end], value) => isBracketPair.test(value[start - 1] + value[end]),
],
}
9 changes: 3 additions & 6 deletions package/src/languages/html.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { languageMap } from "../core"
import { isBracketPair, xmlClosingTag, xmlOpeningTag } from "./patterns"
import { isBracketPair, openBracket, xmlClosingTag, xmlOpeningTag } from "./patterns"

const voidTags = /^(?:area|base|w?br|col|embed|hr|img|input|link|meta|source|track)$/

Expand All @@ -14,16 +14,13 @@ languageMap.markup =
block: ["<!--", "-->"],
},
autoIndent: [
([start], value) =>
isOpening((value = value.slice(0, start))) ||
/[([{][^\n)\]}]*$/.test(value),
([start], value) => isOpening((value = value.slice(0, start))) || openBracket.test(value),
([start, end], value) =>
isBracketPair.test(value[start - 1] + value[end]) ||
(isOpening(value.slice(0, start)) && xmlClosingTag.test(value.slice(end))),
],
autoCloseTags([start, end], value) {
const tagName =
start == end && (value.slice(0, start) + ">").match(xmlOpeningTag)?.[1]
const tagName = start == end && (value.slice(0, start) + ">").match(xmlOpeningTag)?.[1]
if (tagName && !voidTags.test(tagName)) return `</${tagName}>`
},
}
5 changes: 3 additions & 2 deletions package/src/languages/patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const clikeIndent = /[([{][^\n)\]}]*$|(?:(?:^|[^.])\b(?:if\s*\(.+?\)|else|case.+
isBracketPair = /\[]|\(\)|{}/,
xmlOpeningTag =
/<(?![!\d])([^\s>\/=$<%]+)(?:\s(?:\s*[^\s>\/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+))?)+)?\s*>[ \t]*$/,
xmlClosingTag = /^<\/(?!\d)[^\s>\/=$<%]+\s*>/
xmlClosingTag = /^<\/(?!\d)[^\s>\/=$<%]+\s*>/,
openBracket = /[([{][^\n)\]}]*$/

export { clikeIndent, isBracketPair, xmlOpeningTag, xmlClosingTag }
export { clikeIndent, isBracketPair, xmlOpeningTag, xmlClosingTag, openBracket }
11 changes: 4 additions & 7 deletions package/src/languages/xml.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { languageMap } from "../core"
import { isBracketPair, xmlClosingTag, xmlOpeningTag } from "./patterns"
import { isBracketPair, openBracket, xmlClosingTag, xmlOpeningTag } from "./patterns"

// Same as HTML, but without void tags
languageMap.xml =
Expand All @@ -14,16 +14,13 @@ languageMap.xml =
},
autoIndent: [
([start], value) =>
xmlOpeningTag.test((value = value.slice(0, start))) ||
/[([{][^\n)\]}]*$/.test(value),
xmlOpeningTag.test((value = value.slice(0, start))) || openBracket.test(value),
([start, end], value) =>
isBracketPair.test(value[start - 1] + value[end]) ||
(xmlOpeningTag.test(value.slice(0, start)) &&
xmlClosingTag.test(value.slice(end))),
(xmlOpeningTag.test(value.slice(0, start)) && xmlClosingTag.test(value.slice(end))),
],
autoCloseTags([start, end], value) {
const match =
start == end && (value.slice(0, start) + ">").match(xmlOpeningTag)
const match = start == end && (value.slice(0, start) + ">").match(xmlOpeningTag)
return match ? `</${match[1]}>` : ""
},
}

0 comments on commit 12c1752

Please sign in to comment.