Search in sources :

Example 21 with TIntObjectHashMap

use of gnu.trove.TIntObjectHashMap in project intellij-community by JetBrains.

the class DocumentImpl method stripTrailingSpaces.

/**
   * @return true if stripping was completed successfully, false if the document prevented stripping by e.g. caret(s) being in the way
   */
boolean stripTrailingSpaces(@Nullable final Project project, boolean inChangedLinesOnly, boolean skipCaretLines, @NotNull int[] caretOffsets) {
    if (!isStripTrailingSpacesEnabled) {
        return true;
    }
    List<StripTrailingSpacesFilter> filters = new ArrayList<>();
    for (StripTrailingSpacesFilterFactory filterFactory : StripTrailingSpacesFilterFactory.EXTENSION_POINT.getExtensions()) {
        StripTrailingSpacesFilter filter = filterFactory.createFilter(project, this);
        if (filter == StripTrailingSpacesFilter.NOT_ALLOWED) {
            return true;
        } else if (filter == StripTrailingSpacesFilter.POSTPONED) {
            return false;
        } else {
            filters.add(filter);
        }
    }
    boolean markAsNeedsStrippingLater = false;
    CharSequence text = myText;
    TIntObjectHashMap<List<RangeMarker>> caretMarkers = new TIntObjectHashMap<>(caretOffsets.length);
    try {
        if (skipCaretLines) {
            for (int caretOffset : caretOffsets) {
                if (caretOffset < 0 || caretOffset > getTextLength()) {
                    continue;
                }
                int line = getLineNumber(caretOffset);
                List<RangeMarker> markers = caretMarkers.get(line);
                if (markers == null) {
                    markers = new ArrayList<>();
                    caretMarkers.put(line, markers);
                }
                RangeMarker marker = createRangeMarker(caretOffset, caretOffset);
                markers.add(marker);
            }
        }
        lineLoop: for (int line = 0; line < getLineCount(); line++) {
            LineSet lineSet = getLineSet();
            int maxSpacesToLeave = getMaxSpacesToLeave(line, filters);
            if (inChangedLinesOnly && !lineSet.isModified(line) || maxSpacesToLeave < 0)
                continue;
            int whiteSpaceStart = -1;
            final int lineEnd = lineSet.getLineEnd(line) - lineSet.getSeparatorLength(line);
            int lineStart = lineSet.getLineStart(line);
            for (int offset = lineEnd - 1; offset >= lineStart; offset--) {
                char c = text.charAt(offset);
                if (c != ' ' && c != '\t') {
                    break;
                }
                whiteSpaceStart = offset;
            }
            if (whiteSpaceStart == -1)
                continue;
            if (skipCaretLines) {
                List<RangeMarker> markers = caretMarkers.get(line);
                if (markers != null) {
                    for (RangeMarker marker : markers) {
                        if (marker.getStartOffset() >= 0 && whiteSpaceStart < marker.getStartOffset()) {
                            // mark this as a document that needs stripping later
                            // otherwise the caret would jump madly
                            markAsNeedsStrippingLater = true;
                            continue lineLoop;
                        }
                    }
                }
            }
            final int finalStart = whiteSpaceStart + maxSpacesToLeave;
            if (finalStart < lineEnd) {
                // document must be unblocked by now. If not, some Save handler attempted to modify PSI
                // which should have been caught by assertion in com.intellij.pom.core.impl.PomModelImpl.runTransaction
                DocumentUtil.writeInRunUndoTransparentAction(new DocumentRunnable(DocumentImpl.this, project) {

                    @Override
                    public void run() {
                        deleteString(finalStart, lineEnd);
                    }
                });
            }
            text = myText;
        }
    } finally {
        caretMarkers.forEachValue(markerList -> {
            if (markerList != null) {
                for (RangeMarker marker : markerList) {
                    try {
                        marker.dispose();
                    } catch (Exception e) {
                        LOG.error(e);
                    }
                }
            }
            return true;
        });
    }
    return markAsNeedsStrippingLater;
}
Also used : IntArrayList(com.intellij.util.containers.IntArrayList) ArrayList(java.util.ArrayList) ImmutableCharSequence(com.intellij.util.text.ImmutableCharSequence) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) TIntObjectHashMap(gnu.trove.TIntObjectHashMap) IntArrayList(com.intellij.util.containers.IntArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 22 with TIntObjectHashMap

use of gnu.trove.TIntObjectHashMap in project intellij-community by JetBrains.

the class FrameworkDetectorRegistryImpl method loadDetectors.

private void loadDetectors() {
    Map<String, FrameworkDetector> newDetectors = new HashMap<>();
    for (FrameworkDetector detector : FrameworkDetector.EP_NAME.getExtensions()) {
        newDetectors.put(detector.getDetectorId(), detector);
    }
    myDetectorIds = new TObjectIntHashMap<>();
    final File file = getDetectorsRegistryFile();
    int maxId = REGISTRY_VERSION;
    if (file.exists()) {
        LOG.debug("loading framework detectors registry from " + file.getAbsolutePath());
        List<String> unknownIds = new ArrayList<>();
        boolean versionChanged = false;
        try {
            DataInputStream input = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
            try {
                input.readInt();
                myDetectorsVersion = input.readInt();
                int size = input.readInt();
                while (size-- > REGISTRY_VERSION) {
                    final String stringId = input.readUTF();
                    int intId = input.readInt();
                    maxId = Math.max(maxId, intId);
                    final int version = input.readInt();
                    final FrameworkDetector detector = newDetectors.remove(stringId);
                    if (detector != null) {
                        if (version != detector.getDetectorVersion()) {
                            LOG.info("Version of framework detector '" + stringId + "' changed: " + version + " -> " + detector.getDetectorVersion());
                            versionChanged = true;
                        }
                        myDetectorIds.put(stringId, intId);
                    } else {
                        unknownIds.add(stringId);
                    }
                }
            } finally {
                input.close();
            }
        } catch (IOException e) {
            LOG.info(e);
        }
        if (!unknownIds.isEmpty()) {
            LOG.debug("Unknown framework detectors: " + unknownIds);
        }
        if (versionChanged || !newDetectors.isEmpty()) {
            if (!newDetectors.isEmpty()) {
                LOG.info("New framework detectors: " + newDetectors.keySet());
            }
            myDetectorsVersion++;
            LOG.info("Framework detection index version changed to " + myDetectorsVersion);
        }
    }
    int nextId = maxId + 1;
    for (String newDetector : newDetectors.keySet()) {
        myDetectorIds.put(newDetector, nextId++);
    }
    myDetectorById = new TIntObjectHashMap<>();
    myDetectorsByFileType = new MultiMap<>();
    for (FrameworkDetector detector : FrameworkDetector.EP_NAME.getExtensions()) {
        final int id = myDetectorIds.get(detector.getDetectorId());
        myDetectorsByFileType.putValue(detector.getFileType(), id);
        myDetectorById.put(id, detector);
        LOG.debug("'" + detector.getDetectorId() + "' framework detector: id = " + id);
    }
}
Also used : TObjectIntHashMap(gnu.trove.TObjectIntHashMap) TIntObjectHashMap(gnu.trove.TIntObjectHashMap) FrameworkDetector(com.intellij.framework.detection.FrameworkDetector)

Aggregations

TIntObjectHashMap (gnu.trove.TIntObjectHashMap)22 ArrayList (java.util.ArrayList)4 TObjectIntHashMap (gnu.trove.TObjectIntHashMap)3 HashMap (java.util.HashMap)3 Nullable (org.jetbrains.annotations.Nullable)3 FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 MultiMap (com.intellij.util.containers.MultiMap)2 TIntArrayList (gnu.trove.TIntArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 Map (java.util.Map)2 Random (java.util.Random)2 NotNull (org.jetbrains.annotations.NotNull)2 VisibleForTesting (com.android.annotations.VisibleForTesting)1 IntArrayWrapper (com.android.ide.common.resources.IntArrayWrapper)1 ResourceType (com.android.resources.ResourceType)1 AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)1 Pair (com.android.util.Pair)1 JavaChainLookupElement (com.intellij.codeInsight.completion.JavaChainLookupElement)1