Skip to content

Commit

Permalink
Merge pull request #1174 from astashov/fix-link-to-crossdart-line
Browse files Browse the repository at this point in the history
Fix line number in the "Link to Crossdart" link
  • Loading branch information
keertip authored Jun 9, 2016
2 parents 1430d4f + 9265065 commit 5b93362
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 33 deletions.
8 changes: 7 additions & 1 deletion bin/dartdoc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import 'package:dartdoc/src/config.dart';
import 'package:dartdoc/src/package_meta.dart';
import 'package:path/path.dart' as path;
import 'package:stack_trace/stack_trace.dart';
import 'package:analyzer/src/generated/sdk.dart';
import 'package:analyzer/src/generated/sdk_io.dart';
import 'package:analyzer/src/generated/java_io.dart';

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

DartSdk sdk = new DirectoryBasedDartSdk(new JavaFile(sdkDir.path));

initializeConfig(
addCrossdart: addCrossdart,
includeSource: includeSource,
inputDir: inputDir);
inputDir: inputDir,
sdkVersion: sdk.sdkVersion);

var dartdoc = new DartDoc(inputDir, excludeLibraries, sdkDir, generators,
outputDir, packageMeta, includeLibraries,
Expand Down
11 changes: 8 additions & 3 deletions lib/src/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ class Config {
final Directory inputDir;
final bool addCrossdart;
final bool includeSource;
Config._(this.inputDir, this.addCrossdart, this.includeSource);
final String sdkVersion;
Config._(
this.inputDir, this.addCrossdart, this.includeSource, this.sdkVersion);
}

Config _config;
Config get config => _config;

void initializeConfig(
{Directory inputDir, bool addCrossdart: false, bool includeSource: true}) {
_config = new Config._(inputDir, addCrossdart, includeSource);
{Directory inputDir,
String sdkVersion,
bool addCrossdart: false,
bool includeSource: true}) {
_config = new Config._(inputDir, addCrossdart, includeSource, sdkVersion);
}
59 changes: 38 additions & 21 deletions lib/src/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2001,21 +2001,8 @@ abstract class SourceCodeMixin {
}

String get _crossdartUrl {
if (_lineNumber != null && _sourceFilePath != null) {
String packageName = library.package.isSdk ? "sdk" : library.package.name;
String packageVersion = library.package.version;
var root = library.package.packageMeta.resolvedDir;
if (!library.package.isSdk) {
root += "/lib";
}
root = root.replaceAll("\\", "/");
var sourceFilePath = new File(_sourceFilePath)
.resolveSymbolicLinksSync()
.replaceAll("\\", "/")
.replaceAll(root, "")
.replaceAll(new RegExp(r"^/*"), "");
String url =
"//www.crossdart.info/p/$packageName/$packageVersion/$sourceFilePath.html";
if (_lineNumber != null && _crossdartPath != null) {
String url = "//www.crossdart.info/p/${_crossdartPath}.html";
return "${url}#line-${_lineNumber}";
} else {
return null;
Expand All @@ -2025,19 +2012,49 @@ abstract class SourceCodeMixin {
int get _lineNumber {
var node = element.computeNode();
if (node is Declaration && (node as Declaration).element != null) {
return lineNumberCache.lineNumber(
(node as Declaration).element.source.fullName, node.offset);
var element = (node as Declaration).element;
var lineNumber = lineNumberCache.lineNumber(
element.source.fullName, element.nameOffset);
return lineNumber + 1;
} else {
return null;
}
}

String get _sourceFilePath {
String get _crossdartPath {
var node = element.computeNode();
if (node is Declaration && (node as Declaration).element != null) {
return ((node as Declaration).element.source as FileBasedSource)
.file
.toString();
var source = ((node as Declaration).element.source as FileBasedSource);
var file = source.file.toString();
var uri = source.uri.toString();
var packageMeta = library.package.packageMeta;
if (uri.startsWith("package:")) {
var splittedUri =
uri.replaceAll(new RegExp(r"^package:"), "").split("/");
var packageName = splittedUri.first;
var packageVersion;
if (packageName == packageMeta.name) {
packageVersion = packageMeta.version;
} else {
var match = new RegExp(
".pub-cache/(hosted/pub.dartlang.org|git)/${packageName}-([^/]+)")
.firstMatch(file);
if (match != null) {
packageVersion = match[2];
}
}
if (packageVersion != null) {
return "${packageName}/${packageVersion}/${splittedUri.skip(1).join("/")}";
} else {
return null;
}
} else if (uri.startsWith("dart:")) {
var packageName = "sdk";
var packageVersion = config.sdkVersion;
return "${packageName}/${packageVersion}/lib/${uri.replaceAll(new RegExp(r"^dart:"), "")}";
} else {
return null;
}
} else {
return null;
}
Expand Down
8 changes: 0 additions & 8 deletions test/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -948,14 +948,6 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
});

group(".crossdartHtmlTag()", () {
test('it returns a Crossdart link when Crossdart support is enabled', () {
initializeConfig(addCrossdart: true);
String packageName = m1.library.package.name;
String packageVersion = m1.library.package.version;
expect(m1.crossdartHtmlTag,
contains("//www.crossdart.info/p/$packageName/$packageVersion"));
});

test('it returns an empty string when Crossdart support is disabled', () {
initializeConfig(addCrossdart: false);
expect(m1.crossdartHtmlTag, "");
Expand Down

0 comments on commit 5b93362

Please sign in to comment.