本文整理匯總了TypeScript中paperclip.parseModuleSource函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript parseModuleSource函數的具體用法?TypeScript parseModuleSource怎麽用?TypeScript parseModuleSource使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了parseModuleSource函數的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: getModuleId
export const getComponentsFromSourceContent = (content: string, filePath: string, state: ApplicationState): RegisteredComponent[] => {
const moduleId = getModuleId(filePath);
const result = parseModuleSource(content);
if (!result.root) {
console.warn(`Syntax error in ${filePath}`);
result.diagnostics.forEach((diagnostic) => {
console.log(diagnostic.message);
})
return [];
}
const module = loadModuleAST(result.root, filePath);
if (module.type !== PCModuleType.COMPONENT) {
return [];
}
return (module as ComponentModule).components.filter(component => !getComponentMetadataItem(component, ComponentMetadataName.INTERNAL)).map(({id, source, previews}) => ({
filePath,
label: id,
location: source.location,
$id: id,
screenshots: previews.map(preview => {
return getComponentScreenshot(id, preview.name, state)
}),
tagName: id,
moduleId: moduleId,
}));
};
示例2: insertSnippet
function* insertSnippet(insertText) {
const editor = vscode.window.activeTextEditor;
const doc = editor.document;
const carretOffset = insertText.indexOf("%|");
insertText = insertText.replace("%|", "");
const docText = doc.getText();
const selection = editor.selection.active || doc.positionAt(docText.length);
const offset = doc.offsetAt(selection);
// smoke test first to make sure that the
const { diagnostics } = parseModuleSource(docText.substr(0, offset) + insertText + docText.substr(offset));
// We COULD just insert the snippet to the end of the document, but
// but it's probably better to fail here and notify the user so that they can correct _their_ mistake.
if (diagnostics.length) {
return false;
}
yield call(async () => {
return editor.edit((edit) => {
edit.insert(
selection,
insertText
);
});
});
if (carretOffset > -1) {
const newPos = doc.positionAt(offset + carretOffset);
editor.selection = new vscode.Selection(newPos, newPos);
}
return true;
}
示例3: parseModuleSource
return moduleFilePaths.map((filePath) => {
const result = parseModuleSource(read(filePath));
if (!result.root) {
return;
}
const module = loadModuleAST(result.root, filePath);
return module;
}).filter(Boolean);
示例4: handleOpenCurrentFileInTandem
function* handleOpenCurrentFileInTandem() {
while(true) {
yield take(OPEN_CURRENT_FILE_IN_TANDEM_EXECUTED);
const state: ExtensionState = yield select();
if (!(yield call(checkCurrentFileIsPaperclip))) {
continue;
}
const activeTextEditor = vscode.window.activeTextEditor;
const filePath = activeTextEditor.document.uri.fsPath;
const { diagnostics, root } = parseModuleSource(activeTextEditor.document.getText(), filePath);
// just show one error for now
if (diagnostics.length) {
vscode.window.showErrorMessage(`Paperclip syntax error: ${diagnostics[0].message}`);
continue;
}
const module = loadModuleAST(root, filePath);
if (module.type === PCModuleType.COMPONENT && !(module as ComponentModule).components.length) {
const pick = yield call(async () => {
return await vscode.window.showInformationMessage("Could not find a component to open with in Tandem. Would you like to create one?", "Yes", "No");
});
if (pick === "Yes") {
yield put(insertNewComponentExecuted());
}
continue;
}
const selectedPreviewOptions: PreviewOption[] = yield call(pickComponentPreviews, module);
if (!selectedPreviewOptions.length) {
continue;
}
// null provided for default preview (first one)
const artboardInfo: ArtboardInfo[] = selectedPreviewOptions.map(({ component, preview }): ArtboardInfo => ({
componentId: component.id,
previewName: preview.name,
width: preview.width,
height: preview.height
}));
yield call(requestOpenTandemIfDisconnected);
yield put(openArtboardsRequested(artboardInfo));
}
}
示例5: doHover
export function doHover(
document: TextDocument,
position: Position,
tagProviders: IHTMLTagProvider[],
config: any,
devToolsPort
): Hover | Promise<Hover> {
const offset = document.offsetAt(position);
// console.log("DOCC");
const { root, diagnostics } = parseModuleSource(document.getText());
// console.log("PARSE MOD SOURCE" + JSON.stringify(root) + JSON.stringify(diagnostics, null, 2));
const expr = root && getExpressionAtPosition(offset, root, (expr) => {
return expr.type === PCExpressionType.SELF_CLOSING_ELEMENT || expr.type === PCExpressionType.ELEMENT;
});
if (!expr) {
return NULL_HOVER;
}
function getTagHover(element: PCSelfClosingElement|PCElement, range: Range, open: boolean): Hover | Promise<Hover> {
const startTag = element.type === PCExpressionType.SELF_CLOSING_ELEMENT ? (element as PCSelfClosingElement) : (element as PCElement).startTag;
const { name: tagName } = startTag;
const tagLower = tagName.toLowerCase();
if (tagLower === "preview") {
const ancestors = getAncestors(element, root);
const component = ancestors[0];
const id = getPCStartTagAttribute(component, "id");
const previewName = getPCStartTagAttribute(element, "name") || component.childNodes.indexOf(element);
if (!id) {
return NULL_HOVER;
}
const hoverFilePath = path.join(TMP_DIRECTORY, `${id.replace(/\-/g, "")}.png`);
return (async () => {
// Hack. Must save files to local FS for some reason.
try {
await new Promise((resolve, reject) => {
request({ url: `http://127.0.0.1:${devToolsPort}/components/${id}/screenshots/${previewName}/latest?maxWidth=300&maxHeight=240`, encoding: null }, (err, response, body) => {
if (err) return reject();
if (response.statusCode !== 200) {
return reject(new Error("not found"));
}
fs.writeFileSync(hoverFilePath, body);
resolve();
})
});
} catch(e) {
return {
contents: `Preview could not be loaded. `,
isTrusted: true
};
}
return {
// timestamp for cache buster
contents: `![component preview](file://${hoverFilePath}?${Date.now()})`
};
})();
}
for (const provider of tagProviders) {
let hover: Hover | null = null;
provider.collectTags((t, label) => {
if (t === tagLower) {
const tagLabel = open ? '<' + tagLower + '>' : '</' + tagLower + '>';
hover = { contents: [{ language: 'html', value: tagLabel }, MarkedString.fromPlainText(label)], range };
}
});
if (hover) {
return hover;
}
}
return NULL_HOVER;
}
function getAttributeHover(tag: string, attribute: string, range: Range): Hover {
tag = tag.toLowerCase();
let hover: Hover = NULL_HOVER;
for (const provider of tagProviders) {
provider.collectAttributes(tag, (attr, type, documentation) => {
if (attribute !== attr) {
return;
}
const contents = [documentation ? MarkedString.fromPlainText(documentation) : `No doc for ${attr}`];
hover = { contents, range };
});
}
return hover;
}
const range = exprLocationToRange(expr.location);
//.........這裏部分代碼省略.........