Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ export const languageSpecs: LanguageSpec[] = [
'watchr',
],
commentStyle: shellStyle,
identCharPattern: /[A-Za-z0-9_!?]/,
},
stylized: 'Ruby',
},
Expand Down Expand Up @@ -379,6 +380,7 @@ export const languageSpecs: LanguageSpec[] = [
...pythonStyle,
docPlacement: 'above the definition',
},
identCharPattern: /[A-Za-z0-9_!?]/,
},
stylized: 'Elixir',
},
Expand Down Expand Up @@ -421,6 +423,7 @@ export const languageSpecs: LanguageSpec[] = [
languageID: 'clojure',
fileExts: ['clj', 'cljs', 'cljx'],
commentStyle: lispStyle,
identCharPattern: /[A-Za-z0-9_\-!?]/,
},
stylized: 'Clojure',
},
Expand All @@ -436,6 +439,7 @@ export const languageSpecs: LanguageSpec[] = [
endRegex: /-}/,
},
},
identCharPattern: /[A-Za-z0-9_']/,
},
stylized: 'Haskell',
},
Expand All @@ -451,6 +455,7 @@ export const languageSpecs: LanguageSpec[] = [
endRegex: /#>/,
},
},
identCharPattern: /[A-Za-z0-9_?]/,
},
stylized: 'PowerShell',
},
Expand All @@ -469,6 +474,7 @@ export const languageSpecs: LanguageSpec[] = [
'el',
],
commentStyle: lispStyle,
identCharPattern: /[A-Za-z0-9_!?]/,
},
stylized: 'Lisp',
},
Expand Down Expand Up @@ -519,6 +525,7 @@ export const languageSpecs: LanguageSpec[] = [
languageID: 'r',
fileExts: ['r', 'R', 'rd', 'rsx'],
commentStyle: { lineRegex: /#'?\s?/ },
identCharPattern: /[A-Za-z0-9_\.]/,
},
stylized: 'R',
},
Expand Down
17 changes: 17 additions & 0 deletions package/src/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
definitionQueries,
findDocstring,
wrapIndentationInCodeBlocks,
findSearchToken,
} from './handler'
import { TextDocument } from 'sourcegraph'
import { pythonStyle, cStyle } from '../../languages'
Expand Down Expand Up @@ -301,3 +302,19 @@ prose
)
})
})

describe('findSearchToken', () => {
it('custom identCharPattern', () => {
assert.deepStrictEqual(
findSearchToken({
text: '(defn skip-ws! []',
position: { line: 0, character: 6 },
identCharPattern: /[A-Za-z0-9_\-!?]/,
}),
{
isComment: false,
searchToken: 'skip-ws!',
}
)
})
})
22 changes: 18 additions & 4 deletions package/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import {
} from 'sourcegraph'

/**
* identCharPattern is used to match identifier tokens
* The default regex for characters allowed in an identifier. It works well for
* C-like languages (C/C++, C#, Java, etc.) but not for languages that allow
* punctuation characters (e.g. Ruby).
*/
const identCharPattern = /[A-Za-z0-9_\-']/
const DEFAULT_IDENT_CHAR_PATTERN = /[A-Za-z0-9_]/

/**
* Selects documents that the extension works on.
Expand Down Expand Up @@ -122,15 +124,18 @@ function resultToLocation({
}
}

function findSearchToken({
export function findSearchToken({
text,
position,
lineRegex,
identCharPattern,
}: {
text: string
position: Position
position: { line: number; character: number }
lineRegex?: RegExp
identCharPattern?: RegExp
}): { searchToken: string; isComment: boolean } | undefined {
identCharPattern = identCharPattern || DEFAULT_IDENT_CHAR_PATTERN
const lines = text.split('\n')
const line = lines[position.line]
let end = line.length
Expand Down Expand Up @@ -572,6 +577,10 @@ export interface HandlerArgs {
*/
docstringIgnore?: RegExp
commentStyle?: CommentStyle
/**
* Regex that matches characters in an identifier.
*/
identCharPattern?: RegExp
sourcegraph: typeof import('sourcegraph')
/**
* Callback that filters the given symbol search results (e.g. to drop
Expand All @@ -589,6 +598,7 @@ export class Handler {
public languageID: string = ''
public fileExts: string[] = []
public commentStyle: CommentStyle | undefined
public identCharPattern: RegExp | undefined
public docstringIgnore: RegExp | undefined
public debugAnnotatedURIs: string[]
public filterDefinitions: FilterDefinitions
Expand All @@ -601,6 +611,7 @@ export class Handler {
languageID,
fileExts = [],
commentStyle,
identCharPattern,
docstringIgnore,
sourcegraph,
filterDefinitions: filterDefinitions = ({ results }) => results,
Expand All @@ -610,6 +621,7 @@ export class Handler {
this.languageID = languageID
this.fileExts = fileExts
this.commentStyle = commentStyle
this.identCharPattern = identCharPattern
this.docstringIgnore = docstringIgnore
this.debugAnnotatedURIs = []
this.filterDefinitions = filterDefinitions
Expand Down Expand Up @@ -704,6 +716,7 @@ export class Handler {
text: fileContent,
position: pos,
lineRegex: this.commentStyle && this.commentStyle.lineRegex,
identCharPattern: this.identCharPattern,
})
if (!tokenResult) {
return null
Expand Down Expand Up @@ -757,6 +770,7 @@ export class Handler {
text: doc.text,
position: pos,
lineRegex: this.commentStyle && this.commentStyle.lineRegex,
identCharPattern: this.identCharPattern,
})
if (!tokenResult) {
return []
Expand Down