| Conditions | 28 |
| Paths | 1348 |
| Total Lines | 81 |
| Code Lines | 66 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like plugin.js ➔ parseCurrentLine often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | (function () { |
||
| 58 | var parseCurrentLine = function (editor, endOffset, delimiter) { |
||
| 59 | var rng, end, start, endContainer, bookmark, text, matches, prev, len, rngText; |
||
| 60 | var autoLinkPattern = Settings.getAutoLinkPattern(editor); |
||
| 61 | var defaultLinkTarget = Settings.getDefaultLinkTarget(editor); |
||
| 62 | if (editor.selection.getNode().tagName === 'A') { |
||
| 63 | return; |
||
| 64 | } |
||
| 65 | rng = editor.selection.getRng(true).cloneRange(); |
||
| 66 | if (rng.startOffset < 5) { |
||
| 67 | prev = rng.endContainer.previousSibling; |
||
| 68 | if (!prev) { |
||
| 69 | if (!rng.endContainer.firstChild || !rng.endContainer.firstChild.nextSibling) { |
||
| 70 | return; |
||
| 71 | } |
||
| 72 | prev = rng.endContainer.firstChild.nextSibling; |
||
| 73 | } |
||
| 74 | len = prev.length; |
||
| 75 | setStart(rng, prev, len); |
||
| 76 | setEnd(rng, prev, len); |
||
| 77 | if (rng.endOffset < 5) { |
||
| 78 | return; |
||
| 79 | } |
||
| 80 | end = rng.endOffset; |
||
| 81 | endContainer = prev; |
||
| 82 | } else { |
||
| 83 | endContainer = rng.endContainer; |
||
| 84 | if (endContainer.nodeType !== 3 && endContainer.firstChild) { |
||
| 85 | while (endContainer.nodeType !== 3 && endContainer.firstChild) { |
||
| 86 | endContainer = endContainer.firstChild; |
||
| 87 | } |
||
| 88 | if (endContainer.nodeType === 3) { |
||
| 89 | setStart(rng, endContainer, 0); |
||
| 90 | setEnd(rng, endContainer, endContainer.nodeValue.length); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | if (rng.endOffset === 1) { |
||
| 94 | end = 2; |
||
| 95 | } else { |
||
| 96 | end = rng.endOffset - 1 - endOffset; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | start = end; |
||
| 100 | do { |
||
| 101 | setStart(rng, endContainer, end >= 2 ? end - 2 : 0); |
||
| 102 | setEnd(rng, endContainer, end >= 1 ? end - 1 : 0); |
||
| 103 | end -= 1; |
||
| 104 | rngText = rng.toString(); |
||
| 105 | } while (rngText !== ' ' && rngText !== '' && rngText.charCodeAt(0) !== 160 && end - 2 >= 0 && rngText !== delimiter); |
||
| 106 | if (rangeEqualsDelimiterOrSpace(rng.toString(), delimiter)) { |
||
| 107 | setStart(rng, endContainer, end); |
||
| 108 | setEnd(rng, endContainer, start); |
||
| 109 | end += 1; |
||
| 110 | } else if (rng.startOffset === 0) { |
||
| 111 | setStart(rng, endContainer, 0); |
||
| 112 | setEnd(rng, endContainer, start); |
||
| 113 | } else { |
||
| 114 | setStart(rng, endContainer, end); |
||
| 115 | setEnd(rng, endContainer, start); |
||
| 116 | } |
||
| 117 | text = rng.toString(); |
||
| 118 | if (text.charAt(text.length - 1) === '.') { |
||
| 119 | setEnd(rng, endContainer, start - 1); |
||
| 120 | } |
||
| 121 | text = rng.toString().trim(); |
||
| 122 | matches = text.match(autoLinkPattern); |
||
| 123 | if (matches) { |
||
| 124 | if (matches[1] === 'www.') { |
||
| 125 | matches[1] = 'http://www.'; |
||
| 126 | } else if (/@$/.test(matches[1]) && !/^mailto:/.test(matches[1])) { |
||
| 127 | matches[1] = 'mailto:' + matches[1]; |
||
| 128 | } |
||
| 129 | bookmark = editor.selection.getBookmark(); |
||
| 130 | editor.selection.setRng(rng); |
||
| 131 | editor.execCommand('createlink', false, matches[1] + matches[2]); |
||
| 132 | if (defaultLinkTarget) { |
||
| 133 | editor.dom.setAttrib(editor.selection.getNode(), 'target', defaultLinkTarget); |
||
| 134 | } |
||
| 135 | editor.selection.moveToBookmark(bookmark); |
||
| 136 | editor.nodeChanged(); |
||
| 137 | } |
||
| 138 | }; |
||
| 139 | var setup = function (editor) { |
||
| 181 |