Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for multiline comment for languages that use JS regex #82

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/* cc:main function */
(* cc:main function *)
let hello = () => "Hello, World!";
4 changes: 3 additions & 1 deletion example-project/src-client/auth/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {

function authenticate(provider) {
return dispatch => {
//cc:signin#1;firebase sign in;+1;call to firebase with auth provider, proceed if success response
/*
* cc:signin#1;firebase sign in;+1;call to firebase with auth provider, proceed if success response
*/
firebaseAuth.signInWithPopup(provider)
.then(result => dispatch(signInSuccess(result)))
.catch(error => dispatch(signInError(error)));
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"redux-persist": "^5.10.0",
"redux-saga": "^0.16.0",
"redux-thunk": "^2.2.0",
"regex-comment": "^1.0.2",
"reselect": "^4.0.0",
"watchpack": "^1.6.0",
"websocket": "1.0.27"
Expand Down
44 changes: 19 additions & 25 deletions src/server/code-parse/language/default/codecrumbs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const compact = require('lodash/compact');
const { CC_NODE_TYPE, NO_TRAIL_FLOW } = require('../../../shared-constants');

const CRUMB = 'codecrumb',
CRUMB_SHORT_HANDLER = 'cc';
const CRUMB_REGEX = /cc|codecrumb/;
import { DEFAULT_REGEX } from "regex-comment/lib/index";

const getCommentNodeValue = node => (node.value || '').trim();

Expand Down Expand Up @@ -38,12 +39,7 @@ const parseCodecrumbComment = (node = {}) => {
return cc;
};

const isCodecrumb = node => {
if (!node) return false;

const comment = getCommentNodeValue(node);
return comment.startsWith(CRUMB) || comment.startsWith(CRUMB_SHORT_HANDLER);
};
const isCodecrumb = node => CRUMB_REGEX.test(getCommentNodeValue(node));

const buildCrumb = (params, crumbNodeLines, path) => ({
type: CC_NODE_TYPE,
Expand All @@ -57,22 +53,21 @@ const buildCrumb = (params, crumbNodeLines, path) => ({
});

const setupGetCommentsFromCode = regex => fileCode => {
if (!fileCode) return [];

return fileCode.split('\n').reduce((comments, item, i) => {
const codeLine = item.trim();
if (!codeLine) return comments;

const matches = regex.exec(codeLine);
if (matches) {
const lineNumber = i + 1;
return [
...comments,
{ value: matches[matches.length - 1], nodeLines: [lineNumber, lineNumber] }
];
}
if (!fileCode) {
return [];
}

const result = fileCode.match(regex) || [];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@idoo match returns more than you need :)
if you run yarn server:two it will look like this
image
while in master it's like this
image
you can see there is an issue with example-project/src-server/app/service/user/session.py

Match returns

[ '#cc:signin#3;updare seesion',
  '',
  'cc:signin#3;updare seesion',
  index: 0,
  input: '#cc:signin#3;updare seesion' ]

there, so after filter by //cc you gonna have double '#cc:signin#3;updare seesion'


return result.reduce((comments, value) => {
value = value.trim()
const index = fileCode.indexOf(value);
const tempString = fileCode.substring(0, index);
const matchLineNumber = tempString.split('\n').length;

return comments;
const commentStringCount = value.split('\n').length
const nodeLines = [matchLineNumber, matchLineNumber + commentStringCount - 1];
return [...comments, { value, nodeLines }]
}, []);
};

Expand All @@ -99,8 +94,7 @@ const setupGetCrumbs = getCommentsFromCode => (fileCode, path) => {
}
};

const DEFAULT_COMMENT_REGEX = /^([^\/\/]*)\/\/(.*)$/;
const getCrumbs = setupGetCrumbs(setupGetCommentsFromCode(DEFAULT_COMMENT_REGEX));
const getCrumbs = setupGetCrumbs(setupGetCommentsFromCode(DEFAULT_REGEX));

module.exports = {
getCrumbs,
Expand Down
4 changes: 2 additions & 2 deletions src/server/code-parse/language/fortran/codecrumbs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { setupGetCrumbs, setupGetCommentsFromCode } = require('../default/codecrumbs');

const FORTRAN_COMMENT_REGEX = /^([^!]*)!(.*)$/;
const getCrumbs = setupGetCrumbs(setupGetCommentsFromCode(FORTRAN_COMMENT_REGEX));
import { FORTRAN_REGEX } from "regex-comment/lib/index";
const getCrumbs = setupGetCrumbs(setupGetCommentsFromCode(FORTRAN_REGEX));

// replace with own implementation if needed
module.exports = {
Expand Down
5 changes: 3 additions & 2 deletions src/server/code-parse/language/haskell/codecrumbs.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const { setupGetCrumbs, setupGetCommentsFromCode } = require('../default/codecrumbs');
import { HASKELL_REGEX } from "regex-comment/lib/index";

const HASKELL_COMMENT_REGEX = /^([^--]*)--(.*)$/;
const getCrumbs = setupGetCrumbs(setupGetCommentsFromCode(HASKELL_COMMENT_REGEX));
const getCrumbs = setupGetCrumbs(setupGetCommentsFromCode(HASKELL_REGEX));

// replace with own implementation if needed
module.exports = {
getCrumbs
};
console.log('//foo')
4 changes: 2 additions & 2 deletions src/server/code-parse/language/lua/codecrumbs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { setupGetCrumbs, setupGetCommentsFromCode } = require('../default/codecrumbs');

const LUA_COMMENT_REGEX = /^([^--]*)--(.*)$/;
const getCrumbs = setupGetCrumbs(setupGetCommentsFromCode(LUA_COMMENT_REGEX));
import { LUA_REGEX } from "regex-comment/lib/index";
const getCrumbs = setupGetCrumbs(setupGetCommentsFromCode(LUA_REGEX));

// replace with own implementation if needed
module.exports = {
Expand Down
5 changes: 2 additions & 3 deletions src/server/code-parse/language/ocaml/codecrumbs.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const { setupGetCrumbs, setupGetCommentsFromCode } = require('../default/codecrumbs');

const OCAML_COMMENT_REGEX = /\/\*(.*?)\*\//;

const getCrumbs = setupGetCrumbs(setupGetCommentsFromCode(OCAML_COMMENT_REGEX));
import { OCAML_REGEX } from "regex-comment/lib/index";
const getCrumbs = setupGetCrumbs(setupGetCommentsFromCode(OCAML_REGEX));

// replace with own implementation if needed
module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions src/server/code-parse/language/perl/codecrumbs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { setupGetCrumbs, setupGetCommentsFromCode } = require('../default/codecrumbs');

const PERL_COMMENT_REGEX = /^([^#]*)#(.*)$/;
const getCrumbs = setupGetCrumbs(setupGetCommentsFromCode(PERL_COMMENT_REGEX));
import { PERL_REGEX } from "regex-comment/lib/index";
const getCrumbs = setupGetCrumbs(setupGetCommentsFromCode(PERL_REGEX));

// replace with own implementation if needed
module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions src/server/code-parse/language/python/codecrumbs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { setupGetCrumbs, setupGetCommentsFromCode } = require('../default/codecrumbs');

const PYTHON_COMMENT_REGEX = /^([^#]*)#(.*)$/;
const getCrumbs = setupGetCrumbs(setupGetCommentsFromCode(PYTHON_COMMENT_REGEX));
import { PYTHON_REGEX } from "regex-comment/lib/index";
const getCrumbs = setupGetCrumbs(setupGetCommentsFromCode(PYTHON_REGEX));

// replace with own implementation if needed
module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions src/server/code-parse/language/ruby/codecrumbs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { setupGetCrumbs, setupGetCommentsFromCode } = require('../default/codecrumbs');

const RUBY_COMMENT_REGEX = /^([^#]*)#(.*)$/;
const getCrumbs = setupGetCrumbs(setupGetCommentsFromCode(RUBY_COMMENT_REGEX));
import { RUBY_REGEX } from "regex-comment/lib/index";
const getCrumbs = setupGetCrumbs(setupGetCommentsFromCode(RUBY_REGEX));

// replace with own implementation if needed
module.exports = {
Expand Down
7 changes: 6 additions & 1 deletion src/server/utils/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ const logAdapter = (msg, force) => (isDebugModeEnabled || force) && console.log(
module.exports = {
getText: e => {
try {
return typeof e === 'object' ? JSON.stringify(e) : e;
if (typeof e === 'object') {
const stringifiedError = JSON.stringify(e);
return stringifiedError === '{}' ? e : stringifiedError;
} else {
return e;
}
} catch (e) {
return `COULD NOT PARSE ERROR ${e}`;
}
Expand Down