import * as fg from "fast-glob" ; import * as _ from "lodash" ; import * as path from "path" ; import * as vscode from "vscode" ; import { getMavenLocalRepository } from "../utils/contextUtils" ; import { COMMAND_COMPLETION_ITEM_SELECTED , INFO_COMPLETION_ITEM_SELECTED } from "./constants" ; import { IMavenCompletionItemProvider } from "./IArtifactProvider" ; import { getSortText } from "./versionUtils" ; class LocalProvider implements IMavenCompletionItemProvider { public async getGroupIdCandidates ( groupIdHint ) { const packageSegments = groupIdHint . split ( "." ) ; packageSegments . pop ( ) ; const validGroupIds = await this . searchForGroupIds ( packageSegments ) ; const commandOnSelection = { title : "selected" , command : COMMAND_COMPLETION_ITEM_SELECTED , arguments : [ { infoName : INFO_COMPLETION_ITEM_SELECTED , completeFor : "groupId" , source : "maven-local" } ] } ; return validGroupIds . map ( gid => { const item = new vscode . CompletionItem ( gid , vscode . CompletionItemKind . Module ) ; item . insertText = gid ; item . detail = "local" ; item . command = commandOnSelection ; return item ; } ) ; } public async getArtifactIdCandidates ( groupId ) { if ( ! groupId ) { return [ ] ; } const validArtifactIds = await this . searchForArtifactIds ( groupId ) ; const commandOnSelection = { title : "selected" , command : COMMAND_COMPLETION_ITEM_SELECTED , arguments : [ { infoName : INFO_COMPLETION_ITEM_SELECTED , completeFor : "artifactId" , source : "maven-local" } ] } ; return validArtifactIds . map ( aid => { const item = new vscode . CompletionItem ( aid , vscode . CompletionItemKind . Field ) ; item . insertText = aid ; item . detail = `GroupId: ${ groupId }` ; ( item as any ) . data = { groupId } ; item . command = commandOnSelection ; return item ; } ) ; } public async getVersionCandidates ( groupId , artifactId ) { if ( ! groupId || ! artifactId ) { return [ ] ; } const validVersions = await this . searchForVersions ( groupId , artifactId ) ; const commandOnSelection = { title : "selected" , command : COMMAND_COMPLETION_ITEM_SELECTED , arguments : [ { infoName : INFO_COMPLETION_ITEM_SELECTED , completeFor : "version" , source : "maven-local" } ] } ; return validVersions . map ( v => { const item = new vscode . CompletionItem ( v , vscode . CompletionItemKind . Constant ) ; item . insertText = v ; item . detail = "local" ; item . sortText = getSortText ( v ) ; item . command = commandOnSelection ; return item ; } ) ; } private async searchForGroupIds ( segments ) { const cwd = path . join ( getMavenLocalRepository ( ) , ... segments ) ; try { const entries = await fg ( [ "**/*/*" , "!**/*.*" ] , { onlyFiles : false , deep : 2 , cwd } ) ; const validSegments = entries . map ( ( e ) => e . substring ( 0 , e . indexOf ( "/" ) ) ) ; const prefix = _ . isEmpty ( segments ) ? "" : [ ... segments , "" ] . join ( "." ) ; return Array . from ( new Set ( validSegments ) ) . map ( seg => `${ prefix }${ seg }` ) ; } catch ( error ) { console . error ( error ) ; return [ ] ; } } private async searchForArtifactIds ( groupId ) { const cwd = path . join ( getMavenLocalRepository ( ) , ... groupId . split ( "." ) ) ; try { const entries = await fg ( [ "**/*.pom" ] , { deep : 2 , cwd } ) ; const validArtifactIds = entries . map ( ( e ) => e . substring ( 0 , e . indexOf ( "/" ) ) ) ; return Array . from ( new Set ( validArtifactIds ) ) ; } catch ( error ) { console . error ( error ) ; return [ ] ; } } private async searchForVersions ( groupId , artifactId ) { const cwd = path . join ( getMavenLocalRepository ( ) , ... groupId . split ( "." ) , artifactId ) ; try { const entries = await fg ( [ "*/*.pom" ] , { deep : 1 , cwd } ) ; return entries . map ( ( e ) => e . substring ( 0 , e . indexOf ( "/" ) ) ) ; } catch ( error ) { console . error ( error ) ; return [ ] ; } } } export const localProvider = new LocalProvider ( ) ;