From bf89b1126b506458a11a0e9e6f55d0df5f9268c7 Mon Sep 17 00:00:00 2001 From: StevilKnevil Date: Thu, 28 Jan 2021 16:24:24 +0000 Subject: [PATCH 1/6] Create .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ From 0aeb8df25536b52e3d1bb55bb8d8384991645ecf Mon Sep 17 00:00:00 2001 From: StevilKnevil Date: Thu, 28 Jan 2021 16:24:39 +0000 Subject: [PATCH 2/6] Initial Commit of hover text --- client/extension.js | 92 +++++++++++++++++++++++++++++++++++++++++++++ package.json | 17 ++++++++- 2 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 client/extension.js diff --git a/client/extension.js b/client/extension.js new file mode 100644 index 0000000..42d8a95 --- /dev/null +++ b/client/extension.js @@ -0,0 +1,92 @@ +const vscode = require('vscode'); +var fetch = require("node-fetch"); + +let urlLookup = new Object(); + +function activate(context) { + + console.log('Congratulations, your extension "star-rod" is now active!'); + // https://www.chrishasz.com/blog/2020/07/28/vscode-how-to-use-local-storage-api/#:~:text=the%20Memento%20class%20in%20the,tune%20how%20you%20store%20data. + //const storage = context.globalState + + + fetch(`https://api.github.com/repos/MarlinFirmware/MarlinDocumentation/contents/_gcode`) + .then(res => res.json()) + .then(body => parseRepoContents(body)) + .catch((error) => console.log(error)); + + disposable = vscode.languages.registerHoverProvider('gcode', { + async provideHover(document, position, token) { + const range = document.getWordRangeAtPosition(position); + const word = document.getText(range); + + // format the word correctly + const regex = /\s*([A-Za-z])(\d+)\s*/ + const matches = word.match(regex); + if(!isNaN(matches[2])) + { + const lookup = matches[1] + matches[2].padStart(3,"0"); + if (urlLookup[lookup] != null) + { + // Can get the contents of the repo here. Need to parse out the hypenated entries (like G000-G001) to make a lookup + //https://api.github.com/repos/MarlinFirmware/MarlinDocumentation/contents/_gcode + const url = urlLookup[lookup]; + /* + const prommise = fetch(url) + .then(res => res.text()) + .then(body => { + const hoverText = parseDocs(body) + return new vscode.Hover({value: "hoverText"}); + }) + .catch((error) => console.log(error)); + + await promise; + */ + + try { + const response = await fetch(url); + const json = await response.text(); + const hoverText = parseDocs(json); + return new vscode.Hover(hoverText); + } catch (error) { + console.log(error); + } + + await promise; + + return promise; + } + } + + return; + } + }); + + context.subscriptions.push(disposable); +} + +function deactivate() { } + +function parseRepoContents(json) +{ + json.forEach(element => { + const filename = element.name.split(".")[0]; + const numberParts = filename.split("-"); + numberParts.forEach(numberPart => { + urlLookup[numberPart] = element.download_url; + }); + }); +} + +function parseDocs(text) +{ + const title = text.match(/title: ([^\n]*)/)[1]; + const brief = text.match(/brief: ([^\n]*)/)[1]; + return new vscode.MarkdownString(`**${title}** \n\n ${brief}`); + +} + +module.exports = { + activate, + deactivate +} \ No newline at end of file diff --git a/package.json b/package.json index 3046b3d..6935c36 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "nc-gcode", "displayName": "nc-gcode", "description": "G-code syntax highlighter", - "version": "0.14.1", + "version": "0.14.2", "publisher": "ML2", "author": { "name": "Milys", @@ -11,11 +11,18 @@ "maintainers": [{ "name": "scottmwyant", "url": "https://github.com/scottmwyant" + }, + { + "name": "Stevil Knevil", + "url": "https://github.com/StevilKnevil" }], "scripts": { "pack": "vsce package" }, "preview": true, + "activationEvents": [ + "onLanguage:gcode" + ], "engines": { "vscode": "^1.19.0" }, @@ -32,7 +39,11 @@ "color": "#E4F2FF", "theme": "light" }, + "main": "./client/extension.js", "contributes": { + "capabilities": { + "hoverProvider": "true" + }, "languages": [{ "id": "gcode", "aliases": [ @@ -88,5 +99,7 @@ "scopeName": "source.gcode", "path": "./gcode.tmLanguage.json" }] - } + }, + "dependencies" : + { "node-fetch" : ">2.6.0" } } \ No newline at end of file From e925998777e1149ce36f8dfa7104901f82453962 Mon Sep 17 00:00:00 2001 From: StevilKnevil Date: Thu, 28 Jan 2021 17:26:24 +0000 Subject: [PATCH 3/6] Only create the cache once --- client/extension.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/client/extension.js b/client/extension.js index 42d8a95..e20eba8 100644 --- a/client/extension.js +++ b/client/extension.js @@ -1,19 +1,21 @@ const vscode = require('vscode'); var fetch = require("node-fetch"); -let urlLookup = new Object(); +var urlLookup; function activate(context) { - console.log('Congratulations, your extension "star-rod" is now active!'); - // https://www.chrishasz.com/blog/2020/07/28/vscode-how-to-use-local-storage-api/#:~:text=the%20Memento%20class%20in%20the,tune%20how%20you%20store%20data. - //const storage = context.globalState + console.log('GCode hover text is active'); - - fetch(`https://api.github.com/repos/MarlinFirmware/MarlinDocumentation/contents/_gcode`) + if (urlLookup == null) + { + console.debug('Fetching cache of documentation'); + urlLookup = new Object(); + fetch(`https://api.github.com/repos/MarlinFirmware/MarlinDocumentation/contents/_gcode`) .then(res => res.json()) .then(body => parseRepoContents(body)) .catch((error) => console.log(error)); + } disposable = vscode.languages.registerHoverProvider('gcode', { async provideHover(document, position, token) { From 529c599317ab7f98234899cd4101a823c65dea7d Mon Sep 17 00:00:00 2001 From: StevilKnevil Date: Thu, 28 Jan 2021 17:26:43 +0000 Subject: [PATCH 4/6] Comments --- client/extension.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/client/extension.js b/client/extension.js index e20eba8..48ca5e3 100644 --- a/client/extension.js +++ b/client/extension.js @@ -25,8 +25,10 @@ function activate(context) { // format the word correctly const regex = /\s*([A-Za-z])(\d+)\s*/ const matches = word.match(regex); + // If the second part is a number then search the lookup for the documentation if(!isNaN(matches[2])) { + // Format the string correctly (i.e. with leading zeros) const lookup = matches[1] + matches[2].padStart(3,"0"); if (urlLookup[lookup] != null) { From 0954a836309e0fffa4ebf0758e4f4a8048b30b8f Mon Sep 17 00:00:00 2001 From: StevilKnevil Date: Thu, 28 Jan 2021 17:27:01 +0000 Subject: [PATCH 5/6] Add a link to the documentation --- client/extension.js | 55 +++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/client/extension.js b/client/extension.js index 48ca5e3..0d00958 100644 --- a/client/extension.js +++ b/client/extension.js @@ -30,39 +30,20 @@ function activate(context) { { // Format the string correctly (i.e. with leading zeros) const lookup = matches[1] + matches[2].padStart(3,"0"); - if (urlLookup[lookup] != null) + const rawInfoUrl = urlLookup[lookup].download_url; + if (rawInfoUrl != null) { - // Can get the contents of the repo here. Need to parse out the hypenated entries (like G000-G001) to make a lookup - //https://api.github.com/repos/MarlinFirmware/MarlinDocumentation/contents/_gcode - const url = urlLookup[lookup]; - /* - const prommise = fetch(url) - .then(res => res.text()) - .then(body => { - const hoverText = parseDocs(body) - return new vscode.Hover({value: "hoverText"}); - }) - .catch((error) => console.log(error)); - - await promise; - */ - try { - const response = await fetch(url); + const response = await fetch(rawInfoUrl); const json = await response.text(); - const hoverText = parseDocs(json); + const docs = parseDocs(json); + const hoverText = `**${docs.title}** \n\n ${docs.brief} \n\n <${urlLookup[lookup].docs_url}>` return new vscode.Hover(hoverText); } catch (error) { console.log(error); } - - await promise; - - return promise; } } - - return; } }); @@ -75,19 +56,29 @@ function parseRepoContents(json) { json.forEach(element => { const filename = element.name.split(".")[0]; - const numberParts = filename.split("-"); - numberParts.forEach(numberPart => { - urlLookup[numberPart] = element.download_url; - }); + const download_url = element.download_url; + const docs_url = `https://marlinfw.org/docs/gcode/${filename}.html`; + + const regex = /([A-Za-z])(\d+)(?:-([A-Za-z])(\d+))?/; + const matches = filename.match(regex); + + // remove the global match + matches.shift(); + + for (i = 0; i <= matches.length/2; i+=2) { + urlLookup[matches[i] + matches[i+1]] = { + download_url: download_url, + docs_url: docs_url, + }; + } }); } function parseDocs(text) { - const title = text.match(/title: ([^\n]*)/)[1]; - const brief = text.match(/brief: ([^\n]*)/)[1]; - return new vscode.MarkdownString(`**${title}** \n\n ${brief}`); - + return { + title:text.match(/title: ([^\n]*)/)[1], + brief:text.match(/brief: ([^\n]*)/)[1]}; } module.exports = { From 25fdf668957d7820671ec4c401d1e8fa73cbf810 Mon Sep 17 00:00:00 2001 From: StevilKnevil Date: Thu, 28 Jan 2021 17:37:09 +0000 Subject: [PATCH 6/6] Extracted out the hover text generation and cached it in memory --- client/extension.js | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/client/extension.js b/client/extension.js index 0d00958..97fa80d 100644 --- a/client/extension.js +++ b/client/extension.js @@ -29,20 +29,8 @@ function activate(context) { if(!isNaN(matches[2])) { // Format the string correctly (i.e. with leading zeros) - const lookup = matches[1] + matches[2].padStart(3,"0"); - const rawInfoUrl = urlLookup[lookup].download_url; - if (rawInfoUrl != null) - { - try { - const response = await fetch(rawInfoUrl); - const json = await response.text(); - const docs = parseDocs(json); - const hoverText = `**${docs.title}** \n\n ${docs.brief} \n\n <${urlLookup[lookup].docs_url}>` - return new vscode.Hover(hoverText); - } catch (error) { - console.log(error); - } - } + const gcode = matches[1] + matches[2].padStart(3,"0"); + return new vscode.Hover(await getHoverText(gcode)); } } }); @@ -52,6 +40,31 @@ function activate(context) { function deactivate() { } +async function getHoverText(gcode) +{ + if (urlLookup[gcode].hoverText == null) + { + // Can we generate the info? + if (urlLookup[gcode].download_url != null) + { + const rawInfoUrl = urlLookup[gcode].download_url; + if (rawInfoUrl != null) + { + try { + const response = await fetch(rawInfoUrl); + const json = await response.text(); + const docs = parseDocs(json); + urlLookup[gcode].hoverText = `**${docs.title}** \n\n ${docs.brief} \n\n <${urlLookup[gcode].docs_url}>` + } catch (error) { + console.log(error); + } + } + } + + } + return urlLookup[gcode].hoverText; +} + function parseRepoContents(json) { json.forEach(element => {