-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: completions for filesystem paths
chore: refactor stylesheet plugin to avoid mutations in most cases
- Loading branch information
1 parent
c1f0d97
commit f88ca6d
Showing
11 changed files
with
393 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"marko-vscode": patch | ||
"@marko/language-server": patch | ||
--- | ||
|
||
Improve completions for file system paths. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import path from "path"; | ||
import { | ||
CompletionItem, | ||
CompletionItemKind, | ||
Range, | ||
TextEdit, | ||
} from "vscode-languageserver"; | ||
import type { Node } from "../../../utils/parser"; | ||
import isDocumentLinkAttr from "../util/is-document-link-attr"; | ||
import fileSystem, { FileType } from "../../../utils/file-system"; | ||
import resolveUrl from "../../../utils/resolve-url"; | ||
import type { CompletionMeta } from "."; | ||
|
||
export async function AttrValue({ | ||
document, | ||
offset, | ||
node, | ||
parsed, | ||
code, | ||
}: CompletionMeta<Node.AttrValue>): Promise<void | CompletionItem[]> { | ||
const attr = node.parent; | ||
if (isDocumentLinkAttr(document, attr.parent, attr)) { | ||
const start = node.value.start + 1; | ||
if (code[start] !== ".") return; // only resolve relative paths | ||
|
||
const end = node.value.end - 1; | ||
const relativeOffset = offset - start; | ||
const rawValue = parsed.read({ | ||
start, | ||
end, | ||
}); | ||
|
||
let segmentStart = rawValue.lastIndexOf("/", relativeOffset); | ||
if (segmentStart === -1) segmentStart = relativeOffset; | ||
|
||
const resolveRequest = rawValue.slice(0, segmentStart) || "."; | ||
const dir = resolveUrl(resolveRequest, document.uri); | ||
|
||
if (dir?.[0] === "/") { | ||
const result: CompletionItem[] = []; | ||
const curDir = | ||
resolveRequest === "." ? dir : resolveUrl(".", document.uri); | ||
const curFile = curDir === dir ? path.basename(document.uri) : undefined; | ||
const replaceRange = Range.create( | ||
document.positionAt(start + segmentStart + 1), | ||
document.positionAt(start + rawValue.length) | ||
); | ||
|
||
for (const [entry, type] of await fileSystem.readDirectory(dir)) { | ||
if (entry[0] !== "." && entry !== curFile) { | ||
const isDir = type === FileType.Directory; | ||
const label = isDir ? `${entry}/` : entry; | ||
result.push({ | ||
label, | ||
kind: isDir ? CompletionItemKind.Folder : CompletionItemKind.File, | ||
textEdit: TextEdit.replace(replaceRange, label), | ||
command: isDir | ||
? { | ||
title: "Suggest", | ||
command: "editor.action.triggerSuggest", | ||
} | ||
: undefined, | ||
}); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { TextDocument } from "vscode-languageserver-textdocument"; | ||
import { type Node, NodeType } from "../../../utils/parser"; | ||
|
||
const linkedAttrs: Map<string, Set<string>> = new Map([ | ||
[ | ||
"src", | ||
new Set([ | ||
"audio", | ||
"embed", | ||
"iframe", | ||
"img", | ||
"input", | ||
"script", | ||
"source", | ||
"track", | ||
"video", | ||
]), | ||
], | ||
["href", new Set(["a", "area", "link"])], | ||
["data", new Set(["object"])], | ||
["poster", new Set(["video"])], | ||
]); | ||
|
||
export default function isDocumentLinkAttr( | ||
doc: TextDocument, | ||
tag: Node.ParentTag, | ||
attr: Node.AttrNode | ||
): attr is Node.AttrNamed & { value: Node.AttrValue } { | ||
return ( | ||
(tag.nameText && | ||
attr.type === NodeType.AttrNamed && | ||
attr.value?.type === NodeType.AttrValue && | ||
/^['"]$/.test(doc.getText()[attr.value.value.start]) && | ||
linkedAttrs | ||
.get(doc.getText().slice(attr.name.start, attr.name.end)) | ||
?.has(tag.nameText)) || | ||
false | ||
); | ||
} |
Oops, something went wrong.