Skip to content

Commit

Permalink
Add generic .AtNames() to support @ completion and export findMatches()
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.r-project.org/R/trunk@84159 00db46b3-68df-0310-9c12-caf00c1e9a41
  • Loading branch information
deepayan committed Apr 4, 2023
1 parent a2b5d1f commit 32fd909
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/library/utils/NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Refer to all C routines by their name prefixed by C_
useDynLib(utils, .registration = TRUE, .fixes = "C_")

export("?", .DollarNames, .S3methods, .romans, Rprof, Rprofmem,
export("?", .AtNames, .DollarNames, .S3methods, .romans, Rprof, Rprofmem,
RShowDoc, RSiteSearch, URLdecode, URLencode, View, adist,
alarm, apropos, aregexec, argsAnywhere, asDateBuilt, askYesNo,
assignInMyNamespace, assignInNamespace, as.roman, as.person,
Expand All @@ -17,7 +17,7 @@ export("?", .DollarNames, .S3methods, .romans, Rprof, Rprofmem,
data.entry, dataentry, de, de.ncols, de.restore, de.setup,
debugger, debugcall, demo, download.file, download.packages,
dump.frames, edit, emacs, example, file_test, file.edit,
fileSnapshot, find, findCRANmirror, fix, fixInNamespace, findLineNum,
fileSnapshot, find, findCRANmirror, findMatches, fix, fixInNamespace, findLineNum,
flush.console, formatOL, formatUL, getAnywhere, getCRANmirrors,
getFromNamespace, getParseData, getParseText, getS3method,
getSrcDirectory, getSrcFilename, getSrcLocation, getSrcref,
Expand Down Expand Up @@ -165,6 +165,7 @@ S3method("unique", "bibentry")
S3method(.DollarNames, default)
S3method(.DollarNames, list)
S3method(.DollarNames, environment)
S3method(.AtNames, default)

export(Rtangle, RtangleSetup, RweaveLatex, RweaveLatexSetup, Stangle,
Sweave, SweaveSyntConv, SweaveSyntaxLatex, SweaveSyntaxNoweb,
Expand Down
21 changes: 16 additions & 5 deletions src/library/utils/R/completion.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ findFuzzyMatches <- function(pattern, values) {
}


findMatches <- function(pattern, values)
findMatches <- function(pattern, values, fuzzy)
{
if (.CompletionEnv$settings[["fuzzy"]])
if (missing(fuzzy)) fuzzy <- .CompletionEnv$settings[["fuzzy"]]
if (fuzzy)
findFuzzyMatches(pattern, values)
else
findExactMatches(pattern, values)
Expand Down Expand Up @@ -160,6 +161,18 @@ fuzzyApropos <- function(what)
findMatches(pattern, ls(x, all.names = TRUE))
}

## generic and default method to generate completion after @

.AtNames <- function(x, pattern)
UseMethod(".AtNames")

.AtNames.default <- function(x, pattern = "") {
if (isS4(x))
findMatches(pattern, methods::slotNames(x))
else
character()
}

## if (is.environment(object))
## {
## ls(object,
Expand Down Expand Up @@ -360,7 +373,6 @@ specialOpCompletionsHelper <- function(op, suffix, prefix)
suffix
else
{
## ## suffix must match names(object) (or ls(object) for environments)
.DollarNames(object, pattern = sprintf("^%s", makeRegexpSafe(suffix)))
}
} else suffix
Expand All @@ -373,8 +385,7 @@ specialOpCompletionsHelper <- function(op, suffix, prefix)
suffix
else
{
findMatches(sprintf("^%s", makeRegexpSafe(suffix)),
methods::slotNames(object))
.AtNames(object, pattern = sprintf("^%s", makeRegexpSafe(suffix)))
}
} else suffix
},
Expand Down
31 changes: 27 additions & 4 deletions src/library/utils/man/rcompgen.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
\alias{rc.status}
\alias{rc.getOption}
\alias{rc.options}
\alias{findMatches}
\alias{.DollarNames}
\alias{.DollarNames.default}
\alias{.DollarNames.list}
\alias{.DollarNames.environment}
\alias{.AtNames}
\alias{.AtNames.default}
\alias{completion}

\title{A Completion Generator for R}
Expand All @@ -36,10 +39,14 @@ rc.getOption(name)
rc.options(\dots)

.DollarNames(x, pattern)
.AtNames(x, pattern)

\S3method{.DollarNames}{default}(x, pattern = "")
\S3method{.DollarNames}{list}(x, pattern = "")
\S3method{.DollarNames}{environment}(x, pattern = "")
\S3method{.AtNames}{default}(x, pattern = "")

findMatches(pattern, values, fuzzy)

}

Expand Down Expand Up @@ -95,7 +102,8 @@ rc.options(\dots)
\item{fuzzy}{ Logical flag. Enables fuzzy matching, where close but
non-exact matches (e.g., with different case) are considered if no
exact matches are found. This feature is experimental and the
details can change. }
details can change. In \code{findMatches}, this argument defaults to
the current setting. }

\item{quotes}{ Logical flag. Enables completion in \R code when inside
quotes. This normally leads to filename completion, but can be
Expand Down Expand Up @@ -123,6 +131,10 @@ rc.options(\dots)
\item{pattern}{ A regular expression. Only matching names are
returned.
}

\item{values}{ character string giving set of candidate values
in which matches are to be found.
}
}

\details{
Expand Down Expand Up @@ -151,9 +163,11 @@ rc.options(\dots)
function call (implicit function calls involving the use of
\code{[}, \code{$}, etc \emph{do not} inhibit evaluation).

Valid completions after the \code{$} extractor are determined by
the generic function \code{.DollarNames}. Some basic methods are
provided, and more can be written for custom classes.
Valid completions after the \code{$} and \code{@} extractors are
determined by the generic functions \code{.DollarNames} and
\code{.AtNames} respectively. A few basic methods are provided,
and more can be written for custom classes. The \code{findMatches}
function can be useful for this purpose.
}

\item{\bold{Completion inside namespaces}:}{
Expand Down Expand Up @@ -244,6 +258,12 @@ rc.options(\dots)
}
}
\code{findMatches} is an utility function that is used internally to
determine matches. It can be used for writing methods for
\code{.DollarNames} or \code{.AtNames}, the main benefit being that it
will take the current \code{fuzzy} setting into account.
}
\value{
Expand Down Expand Up @@ -282,6 +302,9 @@ rc.options(\dots)
\code{rc.getOption} and \code{rc.options} behave much like
\code{\link{getOption}} and \code{\link{options}} respectively.
\code{findMatches} returns values that match the input pattern, taking
the \code{fuzzy} flag into account.
}
Expand Down

0 comments on commit 32fd909

Please sign in to comment.