Skip to content

Commit

Permalink
Check the selection text when operating via the definition provider
Browse files Browse the repository at this point in the history
Closes #12
  • Loading branch information
jtanx committed Mar 29, 2019
1 parent 0d719f6 commit a1e1532
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 67 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Change Log
All notable changes to the "ctagsx" extension will be documented in this file.

## [1.1.1] - 2019-03-30
### Changed
- Preference the current text selection when navigating via the definition provider (F12). This matches the behaviour when searching via Ctrl+T.

## [1.1.0] - 2019-02-14
### Fixed
- Fixed tag searching for VSCode 1.31
Expand Down
138 changes: 74 additions & 64 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function activate(context) {
context.subscriptions.push(disposable)

if (!vscode.workspace.getConfiguration('ctagsx').get('disableDefinitionProvider')) {
disposable = vscode.languages.registerDefinitionProvider({pattern:'**/*'}, {provideDefinition})
disposable = vscode.languages.registerDefinitionProvider({ pattern: '**/*' }, { provideDefinition })
context.subscriptions.push(disposable)
}
}
Expand Down Expand Up @@ -90,78 +90,88 @@ function findCTags(context, tag) {
}

ctagz.findCTagsBSearch(searchPath, tag)
.then(result => {
const options = result.results.map(tag => {
if (!path.isAbsolute(tag.file)) {
tag.file = path.join(path.dirname(result.tagsFile), tag.file)
}
tag.description = tag.kind || ''
tag.label = tag.file
tag.detail = tag.address.pattern || `Line ${tag.address.lineNumber}`
return tag
})
.then(result => {
const options = result.results.map(tag => {
if (!path.isAbsolute(tag.file)) {
tag.file = path.join(path.dirname(result.tagsFile), tag.file)
}
tag.description = tag.kind || ''
tag.label = tag.file
tag.detail = tag.address.pattern || `Line ${tag.address.lineNumber}`
return tag
})

if (!options.length) {
if (!result.tagsFile) {
return vscode.window.showWarningMessage(`ctagsx: No tags file found`)
if (!options.length) {
if (!result.tagsFile) {
return vscode.window.showWarningMessage(`ctagsx: No tags file found`)
}
return vscode.window.showInformationMessage(`ctagsx: No tags found for ${tag}`)
} else if (options.length === 1) {
return revealCTags(context, editor, options[0])
} else {
return vscode.window.showQuickPick(options).then(opt => {
return revealCTags(context, editor, opt)
})
}
return vscode.window.showInformationMessage(`ctagsx: No tags found for ${tag}`)
} else if (options.length === 1) {
return revealCTags(context, editor, options[0])
} else {
return vscode.window.showQuickPick(options).then(opt => {
return revealCTags(context, editor, opt)
})
}
})
.catch(err => {
console.log(err.stack)
vscode.window.showErrorMessage(`ctagsx: Search failed: ${err}`)
})
})
.catch(err => {
console.log(err.stack)
vscode.window.showErrorMessage(`ctagsx: Search failed: ${err}`)
})
}

function provideDefinition(document, position, canceller) {
if (document.isUntitled || document.uri.scheme !== 'file') {
console.log('ctagsx: Cannot provide definitions for untitled (unsaved) and/or non-local (non file://) documents')
return Promise.reject()
}
const range = document.getWordRangeAtPosition(position)
if (!range) {
console.log('ctagsx: Cannot provide definition without a valid tag (word range)')
return Promise.reject()

let tag, range
const editor = vscode.window.activeTextEditor
if (editor && editor.document == document && position.isEqual(editor.selection.active)) {
range = editor.selection
tag = editor.document.getText(editor.selection).trim()
}
const tag = document.getText(range)

if (!tag) {
console.log('ctagsx: Cannot provide definition with an empty tag')
return Promise.reject()
range = document.getWordRangeAtPosition(position)
if (!range) {
console.log('ctagsx: Cannot provide definition without a valid tag (word range)')
return Promise.reject()
}
tag = document.getText(range)
if (!tag) {
console.log('ctagsx: Cannot provide definition with an empty tag')
return Promise.reject()
}
}

return ctagz.findCTagsBSearch(document.fileName, tag)
.then(result => {
const options = result.results.map(tag => {
if (!path.isAbsolute(tag.file)) {
tag.file = path.join(path.dirname(result.tagsFile), tag.file)
}
return tag
})
.then(result => {
const options = result.results.map(tag => {
if (!path.isAbsolute(tag.file)) {
tag.file = path.join(path.dirname(result.tagsFile), tag.file)
}
return tag
})

const results = []
return Promise.each(options, item => {
if (canceller.isCancellationRequested) {
return
}
return getLineNumber(item, document, range, canceller).then(sel => {
if (sel) {
results.push(new vscode.Location(vscode.Uri.file(item.file), sel.start))
const results = []
return Promise.each(options, item => {
if (canceller.isCancellationRequested) {
return
}
return getLineNumber(item, document, range, canceller).then(sel => {
if (sel) {
results.push(new vscode.Location(vscode.Uri.file(item.file), sel.start))
}
})
}).then(() => {
return results
})
}).then(() => {
return results
})
})
.catch(err => {
console.log(err.stack)
})
.catch(err => {
console.log(err.stack)
})
}

function jumpBack(context) {
Expand Down Expand Up @@ -239,20 +249,20 @@ function getLineNumberPattern(entry, canceller) {
return eachLine(entry.file, line => {
lineNumber += 1
if ((matchWhole && line === pattern) || line.startsWith(pattern)) {
found = true
charPos = Math.max(line.indexOf(entry.name), 0)
console.log(`ctagsx: Found '${pattern}' at ${lineNumber}:${charPos}`)
return false
found = true
charPos = Math.max(line.indexOf(entry.name), 0)
console.log(`ctagsx: Found '${pattern}' at ${lineNumber}:${charPos}`)
return false
} else if (canceller && canceller.isCancellationRequested) {
console.log('ctagsx: Cancelled pattern searching')
return false
}
})
.then(() => {
if (found) {
return new vscode.Selection(lineNumber - 1, charPos, lineNumber - 1, charPos)
}
})
.then(() => {
if (found) {
return new vscode.Selection(lineNumber - 1, charPos, lineNumber - 1, charPos)
}
})
}

/**
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a1e1532

Please sign in to comment.