Skip to content

Commit

Permalink
move editor url to a new file
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthkp committed Jul 15, 2023
1 parent db03f88 commit 8f41043
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
29 changes: 5 additions & 24 deletions packages/babel-plugin-open-source/babel.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { declare } = require('@babel/helper-plugin-utils');
const { types: t } = require('@babel/core');
const dotenv = require('dotenv');
const getGitHubUrl = require('./github-url.js');
const getEditorUrl = require('./editor-url.js');

const scriptLocation = 'babel-plugin-open-source/script.js';

Expand All @@ -16,7 +16,7 @@ module.exports = declare((api) => {
enter: (_, state) => {
state.file.set('hasJSX', false);

const editorInOpts = state.opts && state.opts.editor ? state.opts.editor.toLowerCase() : 'vscode'
const editorInOpts = state.opts && state.opts.editor ? state.opts.editor.toLowerCase() : 'vscode';
let editor = process.env.NODE_ENV === 'production' ? 'github' : editorInOpts;
const editorInENV = dotenvConfig && dotenvConfig.parsed && dotenvConfig.parsed.BABEL_OPEN_SOURCE_EDITOR;
if (editorInENV) editor = editorInENV;
Expand All @@ -42,7 +42,6 @@ module.exports = declare((api) => {
if (state.file.get('skip')) return;

const location = path.container.openingElement.loc;
let url = null;

// the element was generated and doesn't have location information
if (!location) return;
Expand All @@ -63,30 +62,12 @@ module.exports = declare((api) => {
}

const editor = state.file.get('editor');
const url = getEditorUrl(editor, state.filename, location.start.line);

if (editor === 'sublime') {
// https://macromates.com/blog/2007/the-textmate-url-scheme/
// https://github.com/ljubadr/sublime-protocol-win
url = `subl://open?url=file://${state.filename}&line=${location.start.line}`;
} else if (editor === 'phpstorm') {
// https://github.com/siddharthkp/babel-plugin-open-source/issues/6#issue-999132767
url = `phpstorm://open?file=${state.filename}&line=${location.start.line}`;
} else if (editor === 'atom') {
// https://flight-manual.atom.io/hacking-atom/sections/handling-uris/#core-uris
url = `atom://core/open/file?filename=${state.filename}&line=${location.start.line}`;
} else if (editor === 'vscode-insiders') {
url = `vscode-insiders://file/${state.filename}:${location.start.line}`;
} else if (editor === 'github') {
url = getGitHubUrl(state.filename, location.start.line);
} else {
url = `vscode://file/${state.filename}:${location.start.line}`;
}
const sourceData = JSON.stringify({ url });
const sourceNode = t.jsxExpressionContainer(t.templateLiteral([t.templateElement({ raw: sourceData })], []))
const sourceNode = t.jsxExpressionContainer(t.templateLiteral([t.templateElement({ raw: sourceData })], []));

path.container.openingElement.attributes.push(
t.jsxAttribute(t.jsxIdentifier('data-source'), sourceNode)
);
path.container.openingElement.attributes.push(t.jsxAttribute(t.jsxIdentifier('data-source'), sourceNode));
}
};

Expand Down
23 changes: 23 additions & 0 deletions packages/babel-plugin-open-source/editor-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const getGitHubUrl = require('./github-url.js');

const getEditorUrl = (editor, filename, line) => {
if (editor === 'sublime') {
// https://macromates.com/blog/2007/the-textmate-url-scheme/
// https://github.com/ljubadr/sublime-protocol-win
return `subl://open?url=file://${filename}&line=${line}`;
} else if (editor === 'phpstorm') {
// https://github.com/siddharthkp/babel-plugin-open-source/issues/6#issue-999132767
return `phpstorm://open?file=${filename}&line=${line}`;
} else if (editor === 'atom') {
// https://flight-manual.atom.io/hacking-atom/sections/handling-uris/#core-uris
return `atom://core/open/file?filename=${filename}&line=${line}`;
} else if (editor === 'vscode-insiders') {
return `vscode-insiders://file/${filename}:${line}`;
} else if (editor === 'github') {
return getGitHubUrl(filename, line);
} else {
return `vscode://file/${filename}:${line}`;
}
};

module.exports = getEditorUrl;
2 changes: 1 addition & 1 deletion packages/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"react-dom": "17.0.2"
},
"devDependencies": {
"babel-plugin-open-source": "1.3.1",
"babel-plugin-open-source": "1.3.4",
"dotenv": "^16.0.0",
"eslint": "7.32.0",
"eslint-config-next": "11.1.2",
Expand Down

1 comment on commit 8f41043

@vercel
Copy link

@vercel vercel bot commented on 8f41043 Jul 15, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.