How to generate JSDoc comments for functions when no comment exists? - plugins

I'm trying to create a plugin for JSDoc.
I'm following the documentation (which, ironically, is lacking) and I'm not sure how to do this.
My plugin is loaded properly, and I'm trying a simple example. Here's my plugin (which loads, because I can throw an error from there to stop jsdoc from running):
visitNode: function(node, e, parser, currentSourceName) {
if(node.type === 109){
if(!e.comment || e.comment ==="#undocumented"){
var startComment = '/**',
endComment = '\n*/';
var params = node.getParams(),
paramsComment = '';
for(var i=0; i<params.length; i++){
paramsComment += '\n* #param ' + params[i];
}
e.comment = startComment +
paramsComment +
endComment;
}
}
please note that node.type === 109 is equivalent to Token.FUNCTION, which should be available as per their example here, but Token is undefined in the plugin.
If you know of a better site which explains how to write a JSDoc plugin, then that would be very much appreciated too... thanks

I also had this problem and it seems strange that JSDoc does not have some kind of already made option for that or at least a plugin.
Anyway creating this plugin has solved my problem. I am using JSDoc version 3.4:
'use strict';
exports.handlers = {
symbolFound:function(e) {
if(e.astnode.type === "FunctionDeclaration" ) {
if( (e.comment==="#undocumented")){
e.comment = '/** undocumented */';
}
}
}
};

Related

Neovim - How to filter out text snippets from nvim-lspconfig + nvim-cmp

I'm using NeoVim with autocomplete using nvim-lspconfig and nvim-cmp. I would like to know if there's a way of filtering out text feeds from the autocompletion menu, so that they don't appear in the contextual menu:
In your setup you can exclude any kind if suggestions thanks to this merged PR.
What is happening is the function "entry_filter" is getting called whenever a suggestion for nvim_lsp is being made. in it we return false if the entry is of the kind "text".
local cmp = require "cmp"
cmp.setup {
...
sources = cmp.config.sources({
-- Dont suggest Text from nvm_lsp
{ name = "nvim_lsp",
entry_filter = function(entry, ctx)
return require("cmp").lsp.CompletionItemKind.Text ~= entry:get_kind()
end },
})
}
Check out nvim-cmp sources list and remove whatever source you don't want to use. Text is quite probably coming from buffer:
cmp.setup({
...
sources = cmp.config.sources({
{ name = 'buffer' }, -- <- remove
{ name = 'nvim_lsp' },
...
})
})

Babel: replaceWithSourceString giving Unexpected token (1:1)

I am trying to replace dynamically "import" statements.
Here is an example that checks if the import ends with a Plus.
module.exports = function(babel) {
return {
visitor: {
ImportDeclaration: function(path, state) {
// import abc from "./logic/+"
if( ! path.node.source.value.endsWith("/+"))
return;
path.replaceWithSourceString('import all from "./logic/all"')
}
}
}
}
This gives an error of
SyntaxError: src/boom.js: Unexpected token (1:1) - make sure this is an expression.
> 1 | (import all from "./logic/all")
The problem is that replaceWithSourceString is wrapping the string in rounded braces.
If I change the replaceWithSourceString to
path.replaceWithSourceString('console.log("Hi")')
and this works.. ¯_(ツ)_/¯
Any and all help you be great
replaceWithSourceString should really be avoided, because it is just not a very good API, as you are seeing. The recommended approach for creating ASTs to insert into the script is to use template. Assuming this is for Babel 7.x, you can do
const importNode = babel.template.statement.ast`import all from "./logic/all"`;
path.replaceWith(importNode);

How to select symbols onWorkspaceSymbol

I am developing an extension for visual studio code using language server protocol, and I am including the support for "Go to symbol in workspace". My problem is that I don't know how to select the matches...
Actually I use this function I wrote:
function IsInside(word1, word2)
{
var ret = "";
var i1 = 0;
var lenMatch =0, maxLenMatch = 0, minLenMatch = word1.length;
for(var i2=0;i2<word2.length;i2++)
{
if(word1[i1]==word2[i2])
{
lenMatch++;
if(lenMatch>maxLenMatch) maxLenMatch = lenMatch;
ret+=word1[i1];
i1++;
if(i1==word1.length)
{
if(lenMatch<minLenMatch) minLenMatch = lenMatch;
// Trying to filter like VSCode does.
return maxLenMatch>=word1.length/2 && minLenMatch>=2? ret : undefined;
}
} else
{
ret+="Z";
if(lenMatch>0 && lenMatch<minLenMatch)
minLenMatch = lenMatch;
lenMatch=0;
}
}
return undefined;
}
That return the sortText if the word1 is inside the word2, undefined otherwise. My problem are cases like this:
My algorithm see that 'aller' is inside CallServer, but the interface does not mark it like expected.
There is a library or something that I must use for this? the code of VSCode is big and complex and I don't know where start looking for this information...
VSCode's API docs for provideWorkspaceSymbols() provide the following guidance (which I don't think your example violates):
The query-parameter should be interpreted in a relaxed way as the editor will apply its own highlighting and scoring on the results. A good rule of thumb is to match case-insensitive and to simply check that the characters of query appear in their order in a candidate symbol. Don't use prefix, substring, or similar strict matching.
These docs were added in response to this discussion, where somebody had very much the same issue as you.
Having a brief look at VSCode sources, internally it seems to use filters.matchFuzzy2() for the highlighting (see here and here). I don't think it's exposed in the API, so you would probably have to copy it if you wanted the behavior to match exactly.

Cloud9 & Meteor.js undefined global var

I'm trying to create the Leaderboard on Cloud9. But I get the error: PlayersList not defined... in the editor. The app is working, but then it's code in editor underlining all 'not defind PlayersList'
The code:
PlayersList = new Mongo.Collection('players');
if(Meteor.isClient){
Template.leaderboard.helpers({
'player': function(){
return PlayersList.find({}, {sort: {score: -1, name: 1}});
},
'selectedClass': function(){
var playerId = this._id;
var selectedPlayer = Session.get('selectedPlayer');
if(selectedPlayer === playerId){
return 'selected';
}
},
'showSelectedPlayer': function(){
var selectedPlayer = Session.get('selectedPlayer');
return PlayersList.findOne(selectedPlayer);
}
});
Cloud9's editor uses ESLint, and using foo = 22 makes it think that there's a missing statement like var foo; somewhere. You can either choose to ignore this, or fix it as follows:
Add /*eslint-env meteor */ to the top so it doesn't give warnings about Meteor globals, and maybe you'll also need to add /* globals Player */ added too in case the error still stays. (I haven't tested this out, please let me know how it goes so I can improve the answer)
I solved the problem with a little workaround. I added coffeescript and then I used the # symbol on the global like you should, when you define a collection with coffeescript. And that was solving it for me. It works fine. As I opened the app in an new browser window, posts were available in the console.
Example:
#Posts = new Mongo.Collection('posts')

Magento error when trying to duplication product

I am using magento 1.7. i have got issue i don't know why this is happen. i just open product in backend for edit then click on duplicate then i got following error
Warning: Illegal string offset 'new_file' in D:\wamp\www\easyshop\app\code\core\Mage\Catalog\Model\Product\Attribute\Backend\Media.php on line 158
when i try following code to debug file:
print_r($newImages);
die;
then i got this following data
Array
(
[/s/a/samsung_galaxy_s2_front1.jpg] => /s/a/samsung_galaxy_s2_front1_4.jpg
[/s/g/sgs2p1.jpg] => /s/g/sgs2p1_4.jpg
[/s/g/sgs2_11.jpg] => /s/g/sgs2_11_4.jpg
[/s/g/sgs2-4386.jpg] => /s/g/sgs2-4386_4.jpg
)
I thing array keys are wrong can you please give solution to solve this problem
I had the same problem on 1.7.02. The solution I found was to change Magento's (IMHO) bugged code.
On Mage_Catalog_Model_Product_Attribute_Backend_Media i've changed the lines where you find:
// For duplicating we need copy original images.
$duplicate = array();
foreach ($value['images'] as &$image) {
if (!isset($image['value_id'])) {
continue;
}
$duplicate[$image['value_id']] = $this->_copyImage($image['file']);
$newImages[$image['file']] = $duplicate[$image['value_id']];
}
for:
// For duplicating we need copy original images.
$duplicate = array();
foreach ($value['images'] as &$image) {
if (!isset($image['value_id'])) {
continue;
}
$duplicate[$image['value_id']] = $this->_copyImage($image['file']);
$newImages[$image['file']] = array();
$newImages[$image['file']]['new_file'] = $duplicate[$image['value_id']];
$newImages[$image['file']]['label'] = $image['label'];
}
It did the trick for me... Images are now being properly duplicated and enabled on new product.