Skip to content

Commit

Permalink
Add command for printing clojredocs to repl window
Browse files Browse the repository at this point in the history
WIP, addressing #689
  • Loading branch information
PEZ committed Oct 24, 2021
1 parent a17ad3a commit 23c6958
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1428,8 +1428,8 @@
"category": "Calva"
},
{
"command": "calva.clojureDocsLookup",
"title": "Lookup ClojureDocs examples",
"command": "calva.printClojureDocsToOutputWindow",
"title": "Print clojuredocs.org examples to OutputWindow",
"enablement": "calva:connected",
"category": "Calva"
},
Expand Down
44 changes: 36 additions & 8 deletions src/clojuredocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type DocsEntry = {
doc: string;
argsLists: string[];
baseUrl: string;
path: string;
urlPath: string;
examples: string[];
notes: string[];
seeAlsos: string[];
Expand All @@ -29,33 +29,61 @@ export function init(cljSession: NReplSession) {
});
}

export async function clojureDocsCiderNReplLookup(session: NReplSession, symbol: string, ns: string): Promise<DocsEntry> {
export async function printClojureDocsToOutputWindow(printDocString = false) {
const docs = await clojureDocsLookup();
if (typeof docs === 'string') {
outputWindow.append(`;; ${docs}`)
}
else {
const exampleSeparatorB = `;; ------- BEGIN EXAMPLE`
const exampleSeparatorE = `;; ------- END EXAMPLE`
const name = `;; ${docs.ns}/${docs.name}`;
const webUrl = `;; ${docs.baseUrl}/${docs.urlPath}`
const doc = docs.doc.split(/\n/).map(line => `;; ${line.replace(/^ {0,3}/, '')}`).join('\n').trim();
const examples = docs.examples.map((example, i) => `${exampleSeparatorB} ${i+1}\n${example.trim()}\n${exampleSeparatorE} ${i+1}`).join('\n\n');
const seeAlsos = docs.seeAlsos.map(also => `${also} - ${docs.baseUrl}/${also.replace(/\?/g, '%3F')}?`).join(`\n`);
outputWindow.append(name);
outputWindow.append(webUrl);
if (printDocString) {
outputWindow.append(doc);
}
outputWindow.append('\n;; Examples:\n');
outputWindow.append(examples);
outputWindow.append('\n;; See also:');
outputWindow.append(seeAlsos);
outputWindow.appendPrompt();
}
}

async function clojureDocsCiderNReplLookup(session: NReplSession, symbol: string, ns: string): Promise<DocsEntry | string> {
const ciderNReplDocs = await session.clojureDocsLookup(ns, symbol);
return ciderNRepl2DocsEntry(ciderNReplDocs.clojuredocs);
if (ciderNReplDocs.clojuredocs) {
return ciderNRepl2DocsEntry(ciderNReplDocs.clojuredocs);
}
return `No docs found for: ${symbol}`;
}

export async function clojureDocsLookup() {
async function clojureDocsLookup(): Promise<DocsEntry | string> {
const doc = util.getDocument({});
const position = vscode.window.activeTextEditor.selection.active;
const symbol = util.getWordAtPosition(doc, position);
const ns = namespace.getNamespace(doc);
const session = replSession.getSession(util.getFileType(doc));

const docs = await clojureDocsCiderNReplLookup(session, symbol, ns);
console.log(docs);
return clojureDocsCiderNReplLookup(session, symbol, ns);
}

function ciderNRepl2DocsEntry(docs: any): DocsEntry {
return {
name: docs.name,
added: docs.added,
baseUrl: 'https://clojuredocs.org/',
baseUrl: 'https://clojuredocs.org',
doc: docs.doc,
argsLists: docs.argsLists,
examples: docs.examples,
notes: docs.notes,
ns: docs.ns,
path: docs.href,
urlPath: docs.href.replace(/^\/?/, ''),
seeAlsos: docs['see-alsos'],
tag: docs.tag,
type: docs.type
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ async function activate(context: vscode.ExtensionContext) {
});
}))
context.subscriptions.push(vscode.commands.registerCommand('calva.continueComment', edit.continueCommentCommand));
context.subscriptions.push(vscode.commands.registerCommand('calva.clojureDocsLookup', clojureDocs.clojureDocsLookup));
context.subscriptions.push(vscode.commands.registerCommand('calva.printClojureDocsToOutputWindow', clojureDocs.printClojureDocsToOutputWindow));

// Initial set of the provided contexts
outputWindow.setContextForOutputWindowActive(false);
Expand Down

0 comments on commit 23c6958

Please sign in to comment.