mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-28 17:04:41 +00:00
* docgen: implement cross-document links Fully implements https://github.com/nim-lang/RFCs/issues/125 Follow-up of: https://github.com/nim-lang/Nim/pull/18642 (for internal links) and https://github.com/nim-lang/Nim/issues/20127. Overview -------- Explicit import-like directive is required, called `.. importdoc::`. (the syntax is % RST, Markdown will use it for a while). Then one can reference any symbols/headings/anchors, as if they were in the local file (but they will be prefixed with a module name or markup document in link text). It's possible to reference anything from anywhere (any direction in `.nim`/`.md`/`.rst` files). See `doc/docgen.md` for full description. Working is based on `.idx` files, hence one needs to generate all `.idx` beforehand. A dedicated option `--index:only` is introduced (and a separate stage for `--index:only` is added to `kochdocs.nim`). Performance note ---------------- Full run for `./koch docs` now takes 185% of the time before this PR. (After: 315 s, before: 170 s on my PC). All the time seems to be spent on `--index:only` run, which takes almost as much (85%) of normal doc run -- it seems that most time is spent on file parsing, turning off HTML generation phase has not helped much. (One could avoid it by specifying list of files that can be referenced and pre-processing only them. But it can become error-prone and I assume that these linke will be **everywhere** in the repository anyway, especially considering https://github.com/nim-lang/RFCs/issues/478. So every `.nim`/`.md` file is processed for `.idx` first). But that's all without significant part of repository converted to cross-module auto links. To estimate impact I checked the time for `doc`ing a few files (after all indexes have been generated), and everywhere difference was **negligible**. E.g. for `lib/std/private/osfiles.nim` that `importdoc`s large `os.idx` and hence should have been a case with relatively large performance impact, but: * After: 0.59 s. * Before: 0.59 s. So Nim compiler works so slow that doc part basically does not matter :-) Testing ------- 1) added `extlinks` test to `nimdoc/` 2) checked that `theindex.html` is still correct 2) fixed broken auto-links for modules that were derived from `os.nim` by adding appropriate ``importdoc`` Implementation note ------------------- Parsing and formating of `.idx` entries is moved into a dedicated `rstidx.nim` module from `rstgen.nim`. `.idx` file format changed: * fields are not escaped in most cases because we need original strings for referencing, not HTML ones (the exception is linkTitle for titles and headings). Escaping happens later -- on the stage of `rstgen` buildIndex, etc. * all lines have fixed number of columns 6 * added discriminator tag as a first column, it always allows distinguish Nim/markup entries, titles/headings, etc. `rstgen` does not rely any more (in most cases) on ad-hoc logic to determine what type each entry is. * there is now always a title entry added at the first line. * add a line number as 6th column * linkTitle (4th) column has a different format: before it was like `module: funcName()`, now it's `proc funcName()`. (This format is also propagated to `theindex.html` and search results, I kept it that way since I like it more though it's discussible.) This column is what used for Nim symbols resolution. * also changed details on column format for headings and titles: "keyword" is original, "linkTitle" is HTML one * fix paths on Windows + more clear code * Update compiler/docgen.nim Co-authored-by: Andreas Rumpf <rumpf_a@web.de> * Handle .md and .nim paths uniformly in findRefFile * handle titles better + more comments * don't allow markup overwrite index title for .nim files Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
346 lines
15 KiB
Nim
346 lines
15 KiB
Nim
#
|
|
#
|
|
# The Nim Compiler
|
|
# (c) Copyright 2018 Andreas Rumpf
|
|
#
|
|
# See the file "copying.txt", included in this
|
|
# distribution, for details about the copyright.
|
|
#
|
|
|
|
## This module contains the ``TMsgKind`` enum as well as the
|
|
## ``TLineInfo`` object.
|
|
|
|
import ropes, tables, pathutils, hashes
|
|
|
|
const
|
|
explanationsBaseUrl* = "https://nim-lang.github.io/Nim"
|
|
# was: "https://nim-lang.org/docs" but we're now usually showing devel docs
|
|
# instead of latest release docs.
|
|
|
|
proc createDocLink*(urlSuffix: string): string =
|
|
# os.`/` is not appropriate for urls.
|
|
result = explanationsBaseUrl
|
|
if urlSuffix.len > 0 and urlSuffix[0] == '/':
|
|
result.add urlSuffix
|
|
else:
|
|
result.add "/" & urlSuffix
|
|
|
|
type
|
|
TMsgKind* = enum
|
|
# fatal errors
|
|
errUnknown, errFatal, errInternal,
|
|
# non-fatal errors
|
|
errIllFormedAstX, errCannotOpenFile,
|
|
errXExpected,
|
|
errRstMissingClosing,
|
|
errRstGridTableNotImplemented,
|
|
errRstMarkdownIllformedTable,
|
|
errRstIllformedTable,
|
|
errRstNewSectionExpected,
|
|
errRstGeneralParseError,
|
|
errRstInvalidDirectiveX,
|
|
errRstInvalidField,
|
|
errRstFootnoteMismatch,
|
|
errRstSandboxedDirective,
|
|
errProveInit, # deadcode
|
|
errGenerated,
|
|
errUser,
|
|
# warnings
|
|
warnCannotOpenFile = "CannotOpenFile", warnOctalEscape = "OctalEscape",
|
|
warnXIsNeverRead = "XIsNeverRead", warnXmightNotBeenInit = "XmightNotBeenInit",
|
|
warnDeprecated = "Deprecated", warnConfigDeprecated = "ConfigDeprecated",
|
|
warnDotLikeOps = "DotLikeOps",
|
|
warnSmallLshouldNotBeUsed = "SmallLshouldNotBeUsed", warnUnknownMagic = "UnknownMagic",
|
|
warnRstRedefinitionOfLabel = "RedefinitionOfLabel",
|
|
warnRstUnknownSubstitutionX = "UnknownSubstitutionX",
|
|
warnRstAmbiguousLink = "AmbiguousLink",
|
|
warnRstBrokenLink = "BrokenLink",
|
|
warnRstLanguageXNotSupported = "LanguageXNotSupported",
|
|
warnRstFieldXNotSupported = "FieldXNotSupported",
|
|
warnRstUnusedImportdoc = "UnusedImportdoc",
|
|
warnRstStyle = "warnRstStyle",
|
|
warnCommentXIgnored = "CommentXIgnored",
|
|
warnTypelessParam = "TypelessParam",
|
|
warnUseBase = "UseBase", warnWriteToForeignHeap = "WriteToForeignHeap",
|
|
warnUnsafeCode = "UnsafeCode", warnUnusedImportX = "UnusedImport",
|
|
warnInheritFromException = "InheritFromException", warnEachIdentIsTuple = "EachIdentIsTuple",
|
|
warnUnsafeSetLen = "UnsafeSetLen", warnUnsafeDefault = "UnsafeDefault",
|
|
warnProveInit = "ProveInit", warnProveField = "ProveField", warnProveIndex = "ProveIndex",
|
|
warnUnreachableElse = "UnreachableElse", warnUnreachableCode = "UnreachableCode",
|
|
warnStaticIndexCheck = "IndexCheck", warnGcUnsafe = "GcUnsafe", warnGcUnsafe2 = "GcUnsafe2",
|
|
warnUninit = "Uninit", warnGcMem = "GcMem", warnDestructor = "Destructor",
|
|
warnLockLevel = "LockLevel", # deadcode
|
|
warnResultShadowed = "ResultShadowed",
|
|
warnInconsistentSpacing = "Spacing", warnCaseTransition = "CaseTransition",
|
|
warnCycleCreated = "CycleCreated", warnObservableStores = "ObservableStores",
|
|
warnStrictNotNil = "StrictNotNil",
|
|
warnResultUsed = "ResultUsed",
|
|
warnCannotOpen = "CannotOpen",
|
|
warnFileChanged = "FileChanged",
|
|
warnSuspiciousEnumConv = "EnumConv",
|
|
warnAnyEnumConv = "AnyEnumConv",
|
|
warnHoleEnumConv = "HoleEnumConv",
|
|
warnCstringConv = "CStringConv",
|
|
warnPtrToCstringConv = "PtrToCstringConv",
|
|
warnEffect = "Effect",
|
|
warnCastSizes = "CastSizes"
|
|
warnImplicitTemplateRedefinition = "ImplicitTemplateRedefinition",
|
|
warnUnnamedBreak = "UnnamedBreak",
|
|
warnStmtListLambda = "StmtListLambda",
|
|
warnBareExcept = "BareExcept",
|
|
warnUser = "User",
|
|
# hints
|
|
hintSuccess = "Success", hintSuccessX = "SuccessX",
|
|
hintCC = "CC",
|
|
hintLineTooLong = "LineTooLong",
|
|
hintXDeclaredButNotUsed = "XDeclaredButNotUsed", hintDuplicateModuleImport = "DuplicateModuleImport",
|
|
hintXCannotRaiseY = "XCannotRaiseY", hintConvToBaseNotNeeded = "ConvToBaseNotNeeded",
|
|
hintConvFromXtoItselfNotNeeded = "ConvFromXtoItselfNotNeeded", hintExprAlwaysX = "ExprAlwaysX",
|
|
hintQuitCalled = "QuitCalled", hintProcessing = "Processing", hintProcessingStmt = "ProcessingStmt", hintCodeBegin = "CodeBegin",
|
|
hintCodeEnd = "CodeEnd", hintConf = "Conf", hintPath = "Path",
|
|
hintConditionAlwaysTrue = "CondTrue", hintConditionAlwaysFalse = "CondFalse", hintName = "Name",
|
|
hintPattern = "Pattern", hintExecuting = "Exec", hintLinking = "Link", hintDependency = "Dependency",
|
|
hintSource = "Source", hintPerformance = "Performance", hintStackTrace = "StackTrace",
|
|
hintGCStats = "GCStats", hintGlobalVar = "GlobalVar", hintExpandMacro = "ExpandMacro",
|
|
hintAmbiguousEnum = "AmbiguousEnum",
|
|
hintUser = "User", hintUserRaw = "UserRaw", hintExtendedContext = "ExtendedContext",
|
|
hintMsgOrigin = "MsgOrigin", # since 1.3.5
|
|
hintDeclaredLoc = "DeclaredLoc", # since 1.5.1
|
|
|
|
const
|
|
MsgKindToStr*: array[TMsgKind, string] = [
|
|
errUnknown: "unknown error",
|
|
errFatal: "fatal error: $1",
|
|
errInternal: "internal error: $1",
|
|
errIllFormedAstX: "illformed AST: $1",
|
|
errCannotOpenFile: "cannot open '$1'",
|
|
errXExpected: "'$1' expected",
|
|
errRstMissingClosing: "$1",
|
|
errRstGridTableNotImplemented: "grid table is not implemented",
|
|
errRstMarkdownIllformedTable: "illformed delimiter row of a markdown table",
|
|
errRstIllformedTable: "Illformed table: $1",
|
|
errRstNewSectionExpected: "new section expected $1",
|
|
errRstGeneralParseError: "general parse error",
|
|
errRstInvalidDirectiveX: "invalid directive: '$1'",
|
|
errRstInvalidField: "invalid field: $1",
|
|
errRstFootnoteMismatch: "number of footnotes and their references don't match: $1",
|
|
errRstSandboxedDirective: "disabled directive: '$1'",
|
|
errProveInit: "Cannot prove that '$1' is initialized.", # deadcode
|
|
errGenerated: "$1",
|
|
errUser: "$1",
|
|
warnCannotOpenFile: "cannot open '$1'",
|
|
warnOctalEscape: "octal escape sequences do not exist; leading zero is ignored",
|
|
warnXIsNeverRead: "'$1' is never read",
|
|
warnXmightNotBeenInit: "'$1' might not have been initialized",
|
|
warnDeprecated: "$1",
|
|
warnConfigDeprecated: "config file '$1' is deprecated",
|
|
warnDotLikeOps: "$1",
|
|
warnSmallLshouldNotBeUsed: "'l' should not be used as an identifier; may look like '1' (one)",
|
|
warnUnknownMagic: "unknown magic '$1' might crash the compiler",
|
|
warnRstRedefinitionOfLabel: "redefinition of label '$1'",
|
|
warnRstUnknownSubstitutionX: "unknown substitution '$1'",
|
|
warnRstAmbiguousLink: "ambiguous doc link $1",
|
|
warnRstBrokenLink: "broken link '$1'",
|
|
warnRstLanguageXNotSupported: "language '$1' not supported",
|
|
warnRstFieldXNotSupported: "field '$1' not supported",
|
|
warnRstUnusedImportdoc: "importdoc for '$1' is not used",
|
|
warnRstStyle: "RST style: $1",
|
|
warnCommentXIgnored: "comment '$1' ignored",
|
|
warnTypelessParam: "", # deadcode
|
|
warnUseBase: "use {.base.} for base methods; baseless methods are deprecated",
|
|
warnWriteToForeignHeap: "write to foreign heap",
|
|
warnUnsafeCode: "unsafe code: '$1'",
|
|
warnUnusedImportX: "imported and not used: '$1'",
|
|
warnInheritFromException: "inherit from a more precise exception type like ValueError, " &
|
|
"IOError or OSError. If these don't suit, inherit from CatchableError or Defect.",
|
|
warnEachIdentIsTuple: "each identifier is a tuple",
|
|
warnUnsafeSetLen: "setLen can potentially expand the sequence, " &
|
|
"but the element type '$1' doesn't have a valid default value",
|
|
warnUnsafeDefault: "The '$1' type doesn't have a valid default value",
|
|
warnProveInit: "Cannot prove that '$1' is initialized. This will become a compile time error in the future.",
|
|
warnProveField: "cannot prove that field '$1' is accessible",
|
|
warnProveIndex: "cannot prove index '$1' is valid",
|
|
warnUnreachableElse: "unreachable else, all cases are already covered",
|
|
warnUnreachableCode: "unreachable code after 'return' statement or '{.noReturn.}' proc",
|
|
warnStaticIndexCheck: "$1",
|
|
warnGcUnsafe: "not GC-safe: '$1'",
|
|
warnGcUnsafe2: "$1",
|
|
warnUninit: "use explicit initialization of '$1' for clarity",
|
|
warnGcMem: "'$1' uses GC'ed memory",
|
|
warnDestructor: "usage of a type with a destructor in a non destructible context. This will become a compile time error in the future.",
|
|
warnLockLevel: "$1", # deadcode
|
|
warnResultShadowed: "Special variable 'result' is shadowed.",
|
|
warnInconsistentSpacing: "Number of spaces around '$#' is not consistent",
|
|
warnCaseTransition: "Potential object case transition, instantiate new object instead",
|
|
warnCycleCreated: "$1",
|
|
warnObservableStores: "observable stores to '$1'",
|
|
warnStrictNotNil: "$1",
|
|
warnResultUsed: "used 'result' variable",
|
|
warnCannotOpen: "cannot open: $1",
|
|
warnFileChanged: "file changed: $1",
|
|
warnSuspiciousEnumConv: "$1",
|
|
warnAnyEnumConv: "$1",
|
|
warnHoleEnumConv: "$1",
|
|
warnCstringConv: "$1",
|
|
warnPtrToCstringConv: "unsafe conversion to 'cstring' from '$1'; this will become a compile time error in the future",
|
|
warnEffect: "$1",
|
|
warnCastSizes: "$1",
|
|
warnImplicitTemplateRedefinition: "template '$1' is implicitly redefined; this is deprecated, add an explicit .redefine pragma",
|
|
warnUnnamedBreak: "Using an unnamed break in a block is deprecated; Use a named block with a named break instead",
|
|
warnStmtListLambda: "statement list expression assumed to be anonymous proc; this is deprecated, use `do (): ...` or `proc () = ...` instead",
|
|
warnBareExcept: "$1",
|
|
warnUser: "$1",
|
|
hintSuccess: "operation successful: $#",
|
|
# keep in sync with `testament.isSuccess`
|
|
hintSuccessX: "$build\n$loc lines; ${sec}s; $mem; proj: $project; out: $output",
|
|
hintCC: "CC: $1",
|
|
hintLineTooLong: "line too long",
|
|
hintXDeclaredButNotUsed: "'$1' is declared but not used",
|
|
hintDuplicateModuleImport: "$1",
|
|
hintXCannotRaiseY: "$1",
|
|
hintConvToBaseNotNeeded: "conversion to base object is not needed",
|
|
hintConvFromXtoItselfNotNeeded: "conversion from $1 to itself is pointless",
|
|
hintExprAlwaysX: "expression evaluates always to '$1'",
|
|
hintQuitCalled: "quit() called",
|
|
hintProcessing: "$1",
|
|
hintProcessingStmt: "$1",
|
|
hintCodeBegin: "generated code listing:",
|
|
hintCodeEnd: "end of listing",
|
|
hintConf: "used config file '$1'",
|
|
hintPath: "added path: '$1'",
|
|
hintConditionAlwaysTrue: "condition is always true: '$1'",
|
|
hintConditionAlwaysFalse: "condition is always false: '$1'",
|
|
hintName: "$1",
|
|
hintPattern: "$1",
|
|
hintExecuting: "$1",
|
|
hintLinking: "$1",
|
|
hintDependency: "$1",
|
|
hintSource: "$1",
|
|
hintPerformance: "$1",
|
|
hintStackTrace: "$1",
|
|
hintGCStats: "$1",
|
|
hintGlobalVar: "global variable declared here",
|
|
hintExpandMacro: "expanded macro: $1",
|
|
hintAmbiguousEnum: "$1",
|
|
hintUser: "$1",
|
|
hintUserRaw: "$1",
|
|
hintExtendedContext: "$1",
|
|
hintMsgOrigin: "$1",
|
|
hintDeclaredLoc: "$1",
|
|
]
|
|
|
|
const
|
|
fatalMsgs* = {errUnknown..errInternal}
|
|
errMin* = errUnknown
|
|
errMax* = errUser
|
|
warnMin* = warnCannotOpenFile
|
|
warnMax* = pred(hintSuccess)
|
|
hintMin* = hintSuccess
|
|
hintMax* = high(TMsgKind)
|
|
rstWarnings* = {warnRstRedefinitionOfLabel..warnRstStyle}
|
|
|
|
type
|
|
TNoteKind* = range[warnMin..hintMax] # "notes" are warnings or hints
|
|
TNoteKinds* = set[TNoteKind]
|
|
|
|
proc computeNotesVerbosity(): array[0..3, TNoteKinds] =
|
|
result[3] = {low(TNoteKind)..high(TNoteKind)} - {warnObservableStores, warnResultUsed, warnAnyEnumConv}
|
|
result[2] = result[3] - {hintStackTrace, hintExtendedContext, hintDeclaredLoc, hintProcessingStmt}
|
|
result[1] = result[2] - {warnProveField, warnProveIndex,
|
|
warnGcUnsafe, hintPath, hintDependency, hintCodeBegin, hintCodeEnd,
|
|
hintSource, hintGlobalVar, hintGCStats, hintMsgOrigin, hintPerformance}
|
|
result[0] = result[1] - {hintSuccessX, hintSuccess, hintConf,
|
|
hintProcessing, hintPattern, hintExecuting, hintLinking, hintCC}
|
|
|
|
const
|
|
NotesVerbosity* = computeNotesVerbosity()
|
|
errXMustBeCompileTime* = "'$1' can only be used in compile-time context"
|
|
errArgsNeedRunOption* = "arguments can only be given if the '--run' option is selected"
|
|
|
|
type
|
|
TFileInfo* = object
|
|
fullPath*: AbsoluteFile # This is a canonical full filesystem path
|
|
projPath*: RelativeFile # This is relative to the project's root
|
|
shortName*: string # short name of the module
|
|
quotedName*: Rope # cached quoted short name for codegen
|
|
# purposes
|
|
quotedFullName*: Rope # cached quoted full name for codegen
|
|
# purposes
|
|
|
|
lines*: seq[string] # the source code of the module
|
|
# used for better error messages and
|
|
# embedding the original source in the
|
|
# generated code
|
|
dirtyFile*: AbsoluteFile # the file that is actually read into memory
|
|
# and parsed; usually "" but is used
|
|
# for 'nimsuggest'
|
|
hash*: string # the checksum of the file
|
|
dirty*: bool # for 'nimfix' / 'nimpretty' like tooling
|
|
when defined(nimpretty):
|
|
fullContent*: string
|
|
FileIndex* = distinct int32
|
|
TLineInfo* = object # This is designed to be as small as possible,
|
|
# because it is used
|
|
# in syntax nodes. We save space here by using
|
|
# two int16 and an int32.
|
|
# On 64 bit and on 32 bit systems this is
|
|
# only 8 bytes.
|
|
line*: uint16
|
|
col*: int16
|
|
fileIndex*: FileIndex
|
|
when defined(nimpretty):
|
|
offsetA*, offsetB*: int
|
|
commentOffsetA*, commentOffsetB*: int
|
|
|
|
TErrorOutput* = enum
|
|
eStdOut
|
|
eStdErr
|
|
|
|
TErrorOutputs* = set[TErrorOutput]
|
|
|
|
ERecoverableError* = object of ValueError
|
|
ESuggestDone* = object of ValueError
|
|
|
|
proc `==`*(a, b: FileIndex): bool {.borrow.}
|
|
|
|
proc hash*(i: TLineInfo): Hash =
|
|
hash (i.line.int, i.col.int, i.fileIndex.int)
|
|
|
|
proc raiseRecoverableError*(msg: string) {.noinline.} =
|
|
raise newException(ERecoverableError, msg)
|
|
|
|
const
|
|
InvalidFileIdx* = FileIndex(-1)
|
|
unknownLineInfo* = TLineInfo(line: 0, col: -1, fileIndex: InvalidFileIdx)
|
|
|
|
type
|
|
Severity* {.pure.} = enum ## VS Code only supports these three
|
|
Hint, Warning, Error
|
|
|
|
const
|
|
trackPosInvalidFileIdx* = FileIndex(-2) # special marker so that no suggestions
|
|
# are produced within comments and string literals
|
|
commandLineIdx* = FileIndex(-3)
|
|
|
|
type
|
|
MsgConfig* = object ## does not need to be stored in the incremental cache
|
|
trackPos*: TLineInfo
|
|
trackPosAttached*: bool ## whether the tracking position was attached to
|
|
## some close token.
|
|
|
|
errorOutputs*: TErrorOutputs
|
|
msgContext*: seq[tuple[info: TLineInfo, detail: string]]
|
|
lastError*: TLineInfo
|
|
filenameToIndexTbl*: Table[string, FileIndex]
|
|
fileInfos*: seq[TFileInfo]
|
|
systemFileIdx*: FileIndex
|
|
|
|
|
|
proc initMsgConfig*(): MsgConfig =
|
|
result.msgContext = @[]
|
|
result.lastError = unknownLineInfo
|
|
result.filenameToIndexTbl = initTable[string, FileIndex]()
|
|
result.fileInfos = @[]
|
|
result.errorOutputs = {eStdOut, eStdErr}
|
|
result.filenameToIndexTbl["???"] = FileIndex(-1)
|