Skip to content

Commit 8e3be79

Browse files
committed
feat: Define and export SnippetId type
1 parent 85060bb commit 8e3be79

21 files changed

+201
-152
lines changed

packages/cli/src/cmds/search/search.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
} from '../../fulltext/FindEvents';
1717
import { openInBrowser } from '../open/openers';
1818
import { buildAppMapIndex, search } from '../../fulltext/appmap-index';
19-
import buildIndex from '../../rpc/explain/buildIndex';
19+
import buildIndexInTempDir from '../../rpc/explain/build-index-in-temp-dir';
2020

2121
export const command = 'search <query>';
2222
export const describe =
@@ -183,7 +183,7 @@ export const handler = async (argv: ArgumentTypes) => {
183183
maxResults,
184184
};
185185

186-
const index = await buildIndex('appmaps', async (indexFile) => {
186+
const index = await buildIndexInTempDir('appmaps', async (indexFile) => {
187187
const db = new sqlite3(indexFile);
188188
const fileIndex = new FileIndex(db);
189189
await buildAppMapIndex(fileIndex, [process.cwd()]);

packages/cli/src/fulltext/FindEvents.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import assert from 'assert';
77

88
import { verbose } from '../utils';
99
import { collectParameters } from './collectParameters';
10-
import { fileNameMatchesFilterPatterns } from './fileNameMatchesFilterPatterns';
10+
import { fileNameMatchesFilterPatterns } from './filter-patterns';
1111

1212
type IndexItem = {
1313
fqid: string;
File renamed without changes.

packages/cli/src/rpc/explain/EventCollector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import FindEvents, {
88
SearchOptions,
99
} from '../../fulltext/FindEvents';
1010
import buildContext from './buildContext';
11-
import { textSearchResultToRpcSearchResult } from './collectContext';
11+
import { textSearchResultToRpcSearchResult } from './textSearchResultToRpcSearchResult';
1212

1313
export default class EventCollector {
1414
appmapIndexes = new Map<string, FindEvents>();

packages/cli/src/rpc/explain/SearchContextCollector.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import EventCollector from './EventCollector';
1111
import indexFiles from './index-files';
1212
import indexSnippets from './index-snippets';
1313
import collectSnippets from './collect-snippets';
14-
import buildIndex from './buildIndex';
14+
import buildIndexInTempDir from './build-index-in-temp-dir';
1515
import { buildAppMapIndex, search } from '../../fulltext/appmap-index';
1616

1717
export default class SearchContextCollector {
@@ -57,7 +57,7 @@ export default class SearchContextCollector {
5757
numResults: this.appmaps.length,
5858
};
5959
} else {
60-
const appmapIndex = await buildIndex('appmaps', async (indexFile) => {
60+
const appmapIndex = await buildIndexInTempDir('appmaps', async (indexFile) => {
6161
const db = new sqlite3(indexFile);
6262
const fileIndex = new FileIndex(db);
6363
await buildAppMapIndex(fileIndex, this.appmapDirectories);
@@ -80,7 +80,7 @@ export default class SearchContextCollector {
8080
log(`[search-context] Matched ${selectedAppMaps.results.length} AppMaps.`);
8181
}
8282

83-
const fileIndex = await buildIndex('files', async (indexFile) => {
83+
const fileIndex = await buildIndexInTempDir('files', async (indexFile) => {
8484
const db = new sqlite3(indexFile);
8585
return await indexFiles(
8686
db,
@@ -96,7 +96,7 @@ export default class SearchContextCollector {
9696
fileIndex.close();
9797
}
9898

99-
const snippetIndex = await buildIndex('snippets', async (indexFile) => {
99+
const snippetIndex = await buildIndexInTempDir('snippets', async (indexFile) => {
100100
const db = new sqlite3(indexFile);
101101
return await indexSnippets(db, fileSearchResults);
102102
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { SearchRpc } from '@appland/rpc';
2+
3+
export default function appmapLocation(appmap: string, event?: SearchRpc.EventMatch): string {
4+
const appmapFile = [appmap, 'appmap.json'].join('.');
5+
const tokens = [appmapFile];
6+
if (event?.eventIds.length) tokens.push(String(event.eventIds[0]));
7+
return tokens.join(':');
8+
}

packages/cli/src/rpc/explain/buildIndex.ts renamed to packages/cli/src/rpc/explain/build-index-in-temp-dir.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type CloseableIndex<T> = {
1515
close: () => void;
1616
};
1717

18-
export default async function buildIndex<T extends Closeable>(
18+
export default async function buildIndexInTempDir<T extends Closeable>(
1919
indexName: string,
2020
builder: (indexFile: string) => Promise<T>
2121
): Promise<CloseableIndex<T>> {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { AppMapFilter, serializeFilter } from '@appland/models';
2+
import { SearchRpc } from '@appland/rpc';
3+
import assert from 'assert';
4+
5+
import { handler as sequenceDiagramHandler } from '../appmap/sequenceDiagram';
6+
import { ContextV2 } from '@appland/navie';
7+
import appmapLocation from './appmap-location';
8+
9+
export default async function buildSequenceDiagram(
10+
result: SearchRpc.SearchResult
11+
): Promise<ContextV2.FileContextItem> {
12+
const codeObjects = result.events.map((event) => event.fqid);
13+
const appmapFilter = new AppMapFilter();
14+
appmapFilter.declutter.context.on = true;
15+
appmapFilter.declutter.context.names = codeObjects;
16+
const filterState = serializeFilter(appmapFilter);
17+
18+
const plantUML = await sequenceDiagramHandler(result.appmap, {
19+
filter: filterState,
20+
format: 'plantuml',
21+
formatOptions: { disableMarkup: true },
22+
});
23+
assert(typeof plantUML === 'string');
24+
return {
25+
directory: result.directory,
26+
location: appmapLocation(result.appmap),
27+
type: ContextV2.ContextItemType.SequenceDiagram,
28+
content: plantUML,
29+
score: result.score,
30+
};
31+
}

packages/cli/src/rpc/explain/buildContext.ts

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { SearchRpc } from '@appland/rpc';
2-
import { AppMapFilter, serializeFilter } from '@appland/models';
3-
import assert from 'assert';
42

5-
import { handler as sequenceDiagramHandler } from '../appmap/sequenceDiagram';
63
import lookupSourceCode from './lookupSourceCode';
74
import { warn } from 'console';
85
import { ContextV2 } from '@appland/navie';
6+
import buildSequenceDiagram from './build-sequence-diagram';
97

108
/**
119
* Processes search results to build sequence diagrams, code snippets, and code object sets. This is the format
@@ -40,32 +38,11 @@ export default async function buildContext(
4038
return tokens.join(':');
4139
};
4240

43-
const buildSequenceDiagram = async (result: SearchRpc.SearchResult) => {
44-
const codeObjects = result.events.map((event) => event.fqid);
45-
const appmapFilter = new AppMapFilter();
46-
appmapFilter.declutter.context.on = true;
47-
appmapFilter.declutter.context.names = codeObjects;
48-
const filterState = serializeFilter(appmapFilter);
49-
50-
const plantUML = await sequenceDiagramHandler(result.appmap, {
51-
filter: filterState,
52-
format: 'plantuml',
53-
formatOptions: { disableMarkup: true },
54-
});
55-
assert(typeof plantUML === 'string');
56-
sequenceDiagrams.push({
57-
directory: result.directory,
58-
location: appmapLocation(result.appmap),
59-
type: ContextV2.ContextItemType.SequenceDiagram,
60-
content: plantUML,
61-
score: result.score,
62-
});
63-
};
64-
6541
const examinedLocations = new Set<string>();
6642
for (const result of searchResults) {
6743
try {
68-
await buildSequenceDiagram(result);
44+
const diagram = await buildSequenceDiagram(result);
45+
sequenceDiagrams.push(diagram);
6946
} catch (e) {
7047
warn(`Failed to build sequence diagram for ${result.appmap}`);
7148
warn(e);

packages/cli/src/rpc/explain/collect-snippets.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ContextV2 } from '@appland/navie';
2-
import { SnippetIndex, SnippetSearchResult } from '@appland/search';
2+
import { parseFileChunkSnippetId, SnippetIndex, SnippetSearchResult } from '@appland/search';
33
import { CHARS_PER_SNIPPET } from './collectContext';
44

55
export default function collectSnippets(
@@ -10,7 +10,9 @@ export default function collectSnippets(
1010
const snippets = snippetIndex.searchSnippets(query, Math.round(charLimit / CHARS_PER_SNIPPET));
1111

1212
const buildLocation = (result: SnippetSearchResult) => {
13-
return `${result.filePath}:${result.startLine}-${result.endLine}`;
13+
const snippetId = parseFileChunkSnippetId(result.snippetId);
14+
const { filePath, startLine } = snippetId;
15+
return [filePath, startLine].filter(Boolean).join(':');
1416
};
1517

1618
return snippets.map((snippet) => ({

0 commit comments

Comments
 (0)