Skip to content

Commit 5b93362

Browse files
authored
Merge pull request #1174 from astashov/fix-link-to-crossdart-line
Fix line number in the "Link to Crossdart" link
2 parents 1430d4f + 9265065 commit 5b93362

File tree

4 files changed

+53
-33
lines changed

4 files changed

+53
-33
lines changed

bin/dartdoc.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import 'package:dartdoc/src/config.dart';
1313
import 'package:dartdoc/src/package_meta.dart';
1414
import 'package:path/path.dart' as path;
1515
import 'package:stack_trace/stack_trace.dart';
16+
import 'package:analyzer/src/generated/sdk.dart';
17+
import 'package:analyzer/src/generated/sdk_io.dart';
18+
import 'package:analyzer/src/generated/java_io.dart';
1619

1720
/// Analyzes Dart files and generates a representation of included libraries,
1821
/// classes, and members. Uses the current directory to look for libraries.
@@ -123,10 +126,13 @@ main(List<String> arguments) async {
123126
var addCrossdart = args['add-crossdart'] as bool;
124127
var includeSource = args['include-source'] as bool;
125128

129+
DartSdk sdk = new DirectoryBasedDartSdk(new JavaFile(sdkDir.path));
130+
126131
initializeConfig(
127132
addCrossdart: addCrossdart,
128133
includeSource: includeSource,
129-
inputDir: inputDir);
134+
inputDir: inputDir,
135+
sdkVersion: sdk.sdkVersion);
130136

131137
var dartdoc = new DartDoc(inputDir, excludeLibraries, sdkDir, generators,
132138
outputDir, packageMeta, includeLibraries,

lib/src/config.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@ class Config {
66
final Directory inputDir;
77
final bool addCrossdart;
88
final bool includeSource;
9-
Config._(this.inputDir, this.addCrossdart, this.includeSource);
9+
final String sdkVersion;
10+
Config._(
11+
this.inputDir, this.addCrossdart, this.includeSource, this.sdkVersion);
1012
}
1113

1214
Config _config;
1315
Config get config => _config;
1416

1517
void initializeConfig(
16-
{Directory inputDir, bool addCrossdart: false, bool includeSource: true}) {
17-
_config = new Config._(inputDir, addCrossdart, includeSource);
18+
{Directory inputDir,
19+
String sdkVersion,
20+
bool addCrossdart: false,
21+
bool includeSource: true}) {
22+
_config = new Config._(inputDir, addCrossdart, includeSource, sdkVersion);
1823
}

lib/src/model.dart

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,21 +2001,8 @@ abstract class SourceCodeMixin {
20012001
}
20022002

20032003
String get _crossdartUrl {
2004-
if (_lineNumber != null && _sourceFilePath != null) {
2005-
String packageName = library.package.isSdk ? "sdk" : library.package.name;
2006-
String packageVersion = library.package.version;
2007-
var root = library.package.packageMeta.resolvedDir;
2008-
if (!library.package.isSdk) {
2009-
root += "/lib";
2010-
}
2011-
root = root.replaceAll("\\", "/");
2012-
var sourceFilePath = new File(_sourceFilePath)
2013-
.resolveSymbolicLinksSync()
2014-
.replaceAll("\\", "/")
2015-
.replaceAll(root, "")
2016-
.replaceAll(new RegExp(r"^/*"), "");
2017-
String url =
2018-
"//www.crossdart.info/p/$packageName/$packageVersion/$sourceFilePath.html";
2004+
if (_lineNumber != null && _crossdartPath != null) {
2005+
String url = "//www.crossdart.info/p/${_crossdartPath}.html";
20192006
return "${url}#line-${_lineNumber}";
20202007
} else {
20212008
return null;
@@ -2025,19 +2012,49 @@ abstract class SourceCodeMixin {
20252012
int get _lineNumber {
20262013
var node = element.computeNode();
20272014
if (node is Declaration && (node as Declaration).element != null) {
2028-
return lineNumberCache.lineNumber(
2029-
(node as Declaration).element.source.fullName, node.offset);
2015+
var element = (node as Declaration).element;
2016+
var lineNumber = lineNumberCache.lineNumber(
2017+
element.source.fullName, element.nameOffset);
2018+
return lineNumber + 1;
20302019
} else {
20312020
return null;
20322021
}
20332022
}
20342023

2035-
String get _sourceFilePath {
2024+
String get _crossdartPath {
20362025
var node = element.computeNode();
20372026
if (node is Declaration && (node as Declaration).element != null) {
2038-
return ((node as Declaration).element.source as FileBasedSource)
2039-
.file
2040-
.toString();
2027+
var source = ((node as Declaration).element.source as FileBasedSource);
2028+
var file = source.file.toString();
2029+
var uri = source.uri.toString();
2030+
var packageMeta = library.package.packageMeta;
2031+
if (uri.startsWith("package:")) {
2032+
var splittedUri =
2033+
uri.replaceAll(new RegExp(r"^package:"), "").split("/");
2034+
var packageName = splittedUri.first;
2035+
var packageVersion;
2036+
if (packageName == packageMeta.name) {
2037+
packageVersion = packageMeta.version;
2038+
} else {
2039+
var match = new RegExp(
2040+
".pub-cache/(hosted/pub.dartlang.org|git)/${packageName}-([^/]+)")
2041+
.firstMatch(file);
2042+
if (match != null) {
2043+
packageVersion = match[2];
2044+
}
2045+
}
2046+
if (packageVersion != null) {
2047+
return "${packageName}/${packageVersion}/${splittedUri.skip(1).join("/")}";
2048+
} else {
2049+
return null;
2050+
}
2051+
} else if (uri.startsWith("dart:")) {
2052+
var packageName = "sdk";
2053+
var packageVersion = config.sdkVersion;
2054+
return "${packageName}/${packageVersion}/lib/${uri.replaceAll(new RegExp(r"^dart:"), "")}";
2055+
} else {
2056+
return null;
2057+
}
20412058
} else {
20422059
return null;
20432060
}

test/model_test.dart

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -948,14 +948,6 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
948948
});
949949

950950
group(".crossdartHtmlTag()", () {
951-
test('it returns a Crossdart link when Crossdart support is enabled', () {
952-
initializeConfig(addCrossdart: true);
953-
String packageName = m1.library.package.name;
954-
String packageVersion = m1.library.package.version;
955-
expect(m1.crossdartHtmlTag,
956-
contains("//www.crossdart.info/p/$packageName/$packageVersion"));
957-
});
958-
959951
test('it returns an empty string when Crossdart support is disabled', () {
960952
initializeConfig(addCrossdart: false);
961953
expect(m1.crossdartHtmlTag, "");

0 commit comments

Comments
 (0)