This repository has been archived by the owner on Dec 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync with facebookarchive/nuclide@87a9370
- Loading branch information
Showing
22 changed files
with
741 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
modules/atom-ide-ui/pkg/atom-ide-console/lib/parseText.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** | ||
* Copyright (c) 2017-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @flow | ||
* @format | ||
*/ | ||
|
||
import {goToLocation} from 'nuclide-commons-atom/go-to-location'; | ||
import * as React from 'react'; | ||
import featureConfig from 'nuclide-commons-atom/feature-config'; | ||
|
||
import {URL_REGEX} from 'nuclide-commons/string'; | ||
const DIFF_PATTERN = '\\b[dD][1-9][0-9]{5,}\\b'; | ||
const TASK_PATTERN = '\\b[tT]\\d+\\b'; | ||
const FILE_PATH_PATTERN = | ||
'([/A-Za-z_-s0-9.-]+[.][A-Za-z]+)(:([0-9]+))?(:([0-9]+))?'; | ||
const CLICKABLE_PATTERNS = `(${DIFF_PATTERN})|(${TASK_PATTERN})|(${ | ||
URL_REGEX.source | ||
})|${FILE_PATH_PATTERN}`; | ||
const CLICKABLE_RE = new RegExp(CLICKABLE_PATTERNS, 'g'); | ||
|
||
function toString(value: mixed): string { | ||
return typeof value === 'string' ? value : ''; | ||
} | ||
|
||
/** | ||
* Parse special entities into links. In the future, it would be great to add a service so that we | ||
* could add new clickable things and to allow providers to mark specific ranges as links to things | ||
* that only they can know (e.g. relative paths output in BUCK messages). For now, however, we'll | ||
* just use some pattern settings and hardcode the patterns we care about. | ||
*/ | ||
export default function parseText( | ||
text: string, | ||
): Array<string | React.Element<any>> { | ||
const chunks = []; | ||
let lastIndex = 0; | ||
let index = 0; | ||
while (true) { | ||
const match = CLICKABLE_RE.exec(text); | ||
if (match == null) { | ||
break; | ||
} | ||
|
||
const matchedText = match[0]; | ||
|
||
// Add all the text since our last match. | ||
chunks.push( | ||
text.slice(lastIndex, CLICKABLE_RE.lastIndex - matchedText.length), | ||
); | ||
lastIndex = CLICKABLE_RE.lastIndex; | ||
|
||
let href; | ||
let handleOnClick; | ||
if (match[1] != null) { | ||
// It's a diff | ||
const url = toString( | ||
featureConfig.get('atom-ide-console.diffUrlPattern'), | ||
); | ||
if (url !== '') { | ||
href = url.replace('%s', matchedText); | ||
} | ||
} else if (match[2] != null) { | ||
// It's a task | ||
const url = toString( | ||
featureConfig.get('atom-ide-console.taskUrlPattern'), | ||
); | ||
if (url !== '') { | ||
href = url.replace('%s', matchedText.slice(1)); | ||
} | ||
} else if (match[3] != null) { | ||
// It's a URL | ||
href = matchedText; | ||
} else if (match[5] != null) { | ||
// It's a file path | ||
href = '#'; | ||
handleOnClick = () => { | ||
goToLocation(match[5], { | ||
line: match[7] ? parseInt(match[7], 10) - 1 : 0, | ||
column: match[9] ? parseInt(match[9], 10) - 1 : 0, | ||
}); | ||
}; | ||
} | ||
|
||
chunks.push( | ||
// flowlint-next-line sketchy-null-string:off | ||
href ? ( | ||
<a | ||
key={`r${index}`} | ||
href={href} | ||
target="_blank" | ||
onClick={handleOnClick}> | ||
{matchedText} | ||
</a> | ||
) : ( | ||
matchedText | ||
), | ||
); | ||
|
||
index++; | ||
} | ||
|
||
// Add any remaining text. | ||
chunks.push(text.slice(lastIndex)); | ||
|
||
return chunks; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.