Skip to content
This repository was archived by the owner on Mar 29, 2022. It is now read-only.

Commit

Permalink
Merge pull request #12 from W95Psp/cloogle-quickpick
Browse files Browse the repository at this point in the history
Cloogle QuickPick
  • Loading branch information
W95Psp authored Feb 11, 2021
2 parents 6c1ce4d + 28e03ca commit 697a66b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 47 deletions.
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@
{
"command": "extension.cpmMakeExec",
"title": "CPM Make & execute"
},
{
"command": "extension.clean.cloogle",
"title": "Search Cloogle"
}
],
"keybindings": [
{
"command": "extension.clean.cloogle",
"key": "shift+alt+c",
"mac": "shift+alt+c",
"win": "shift+alt+c",
"linux": "shift+alt+c"
}
],
"languages": [
Expand Down
25 changes: 21 additions & 4 deletions src/cloogle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import * as https from 'https';
import * as http from 'http';
import {Let} from './tools';
import * as Request from 'request';
import { QuickPickItem } from 'vscode';
import { link } from 'fs';

const getContent = function(url: string, ua: string) {
return new Promise<string>((resolve, reject) =>
Expand All @@ -17,7 +19,7 @@ type cloogleResults =
, more_available: number
};

type cloogleResult
export type cloogleResult
= ['FunctionResult', [cloogleResult_GeneralData, cloogleResult_Function]]
| ['TypeResult', [cloogleResult_GeneralData, cloogleResult_Type]]
| ['ClassResult', [cloogleResult_GeneralData, cloogleResult_Class]]
Expand All @@ -31,8 +33,7 @@ interface cloogleResult_GeneralData {
modul: string;
dcl_line: number;
icl_line: number;
distance: number;
builtin?: boolean;
documentation?: string;
}

interface cloogleResult_Function {
Expand Down Expand Up @@ -84,7 +85,7 @@ export let askCloogleExact = async (name) => {
return result;
}

export let getInterestingStringFrom = ([typeData, [general, specs]]: cloogleResult): string | string[] => {
export let getInterestingStringFrom = ([typeData, [general, specs]]: cloogleResult): string => {
switch(typeData) {
case "FunctionResult":
return (<cloogleResult_Function> specs).func;
Expand All @@ -101,3 +102,19 @@ export let getInterestingStringFrom = ([typeData, [general, specs]]: cloogleResu
return "";
}
}

export interface CloogleQuickPickItem extends QuickPickItem {
itemLocation: string
}

export let toQuickPickItem = (result : cloogleResult) : CloogleQuickPickItem => {
let [typeData, [generalData, specifics]] = result;
let label = getInterestingStringFrom(result);
let description = generalData.documentation;
let link = `https://cloogle.org/src#${generalData.library}/${generalData.modul.replace(/\./g,'/')};line=${generalData.dcl_line}`;
return {
label : label,
description: description,
itemLocation: link
};
}
75 changes: 32 additions & 43 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,56 @@
'use strict';
import {
TextDocument, Position, CancellationToken, ExtensionContext,CompletionItemProvider,CompletionItem,Hover,Range,
TextDocument, Position, CancellationToken, ExtensionContext,CompletionItemProvider,CompletionItem,Range,
languages,window,commands, MarkdownString, DiagnosticCollection, Diagnostic, DiagnosticSeverity,
ShellExecution, Uri, workspace
ShellExecution, Uri, workspace, QuickPickItem, OutputChannel, env
} from 'vscode';
import { normalize, format, parse } from 'path';
import { askCloogle, askCloogleExact, getInterestingStringFrom } from './cloogle';
import { askCloogle, askCloogleExact, getInterestingStringFrom, toQuickPickItem } from './cloogle';
import { Let, Tuple, MakeList } from './tools';
import { cpm, getProjectPath, useBOW } from './cpm';
import { spawn } from 'child_process';

let config_useExportedTypes = workspace.getConfiguration("cleanlang").useExportedTypes === true;
let config_preferExportedTypes = workspace.getConfiguration("cleanlang").preferExportedTypes === true;
let config_preferExportedTypes = workspace.getConfiguration("cleanlang").preferExportedTypes === true;

export function activate(context: ExtensionContext) {
let computedTypes = {};

console.log('CleanLang is active.');

languages.registerHoverProvider('clean', {
async provideHover(document, position, token) {
let editor = window.activeTextEditor;
let regex = /([-~@#$%^?!+*<>\/|&=:.]+|(\w*`|\w+))/;
let rangeVarName = editor.document.getWordRangeAtPosition(position, regex);

if (!rangeVarName) // undefined if regex not match
return;
let varName = editor.document.getText(rangeVarName); // if rangeVarName==undefined, then everything is selected

let precomputed;
if(varName in computedTypes)
precomputed = new Hover(computedTypes[varName]);

if(precomputed && config_preferExportedTypes) {
return precomputed;
} else {
let results = (await askCloogle(varName)).map(result => {
let [TypeData, [GeneralData, Specifics]] = result;

if(GeneralData.builtin && TypeData != 'SyntaxResult')
return [{value: ':: '+varName, language: 'clean'}];

let link = (line: number, icl=false) =>
`https://cloogle.org/src#${GeneralData.library}/${GeneralData.modul.replace(/\./g,'/')}${icl ? ';icl' : ''};line=${line}`;
let head = new MarkdownString(
`[[+]](https://cloogle.org/#${encodeURI(varName)}) ${GeneralData.library}: ${GeneralData.modul} ([dcl:${GeneralData.dcl_line}](${link(GeneralData.dcl_line)}):[icl:${GeneralData.icl_line}](${link(GeneralData.icl_line, true)}))`
);
let listResults:string[] = Let(getInterestingStringFrom(result), t => t instanceof Array ? t : [t]);
[head, ...listResults.map(value => ({value, language: 'clean'}))];
}).flatten();
if(results.length < 1)
return new Hover(results);
}
return precomputed;
let disposable = commands.registerCommand('extension.clean.cloogle', async () : Promise<any> => {
let editor = window.activeTextEditor;
if (!editor) {
return;
}

let selection = editor.selection;
if (selection.start === selection.end) {
return;
}

let text = editor.document.getText(selection).trim();

let results = (await askCloogle(text)).map(result => toQuickPickItem(result));

if (results.length == 0) {
return;
}
window.showQuickPick(results).then((item) => {
if (item) {
env.openExternal(Uri.parse(item.itemLocation));
}
});
});

context.subscriptions.push(disposable);

let regexpParseErrors = /^\s*(Warning|Type error|Error|Overloading .rror|Uniqueness .rror|Parse .rror) \[([\w\.]+),(\d+)(?:;(\d+))?(?:,([^,]*))?]:(.*)((\n\s.*)*)/mg;

let newDiagnosticCollection = () => languages.createDiagnosticCollection('clean');
let diagnostics = newDiagnosticCollection();

let lastOut;
let lastOut: OutputChannel;
let cpmMake = async () => {
let editor = window.activeTextEditor;

Expand Down Expand Up @@ -112,10 +101,10 @@ export function activate(context: ExtensionContext) {
let executable = (cpmResult.match(/^Linking '(.*?)'$/m) || [])[1] || '';

return {executable, out};
};
};

let disposableCpmMake = commands.registerCommand('extension.cpmMake', cpmMake);
let disposableCpmMakeExec = commands.registerCommand('extension.cpmMakeExec', async () => {
let disposableCpmMake = commands.registerCommand('extension.cpmMake', cpmMake);
let disposableCpmMakeExec = commands.registerCommand('extension.cpmMakeExec', async () => {
let result = await cpmMake();

if(result === false)
Expand Down

0 comments on commit 697a66b

Please sign in to comment.